JavaScript Interview Questions And Answers

 Categories of JavaScript Interview Questions

1Q) What is JavaScript?

Ans: JavaScript is a scripting language. It is different from Java language. It is an object-based, lightweight, and cross-platform. It is widely used for client-side validation.

2Q) What is the difference between JavaScript and Jscript?

Ans: Netscape provided the JavaScript language. Microsoft changed the name and called it JScript to avoid the trademark issue. In other words, you can say JScript is the same as JavaScript, but it is provided by Microsoft.

3Q)How to write a hello world example of JavaScript?

Ans: A simple example of JavaScript hello world is given below. You need to place it inside the body tag of HTML.

<script type="text/javascript">  

document.write("JavaScript Hello World!");

</script>

4Q) How to use an external JavaScript file?

Ans:  I am assuming that js file name is message.js, place the following script tag inside the head tag.

<script type="text/javascript" src="message.js"></script>  

5Q) Is JavaScript case sensitive language?

Ans:  Yes.

Learn JavaScript by Tekslate - Fastest growing sector in the industry. Explore Online "JavaScript Training" and course is aligned with industry needs & developed by industry veterans. Tekslate will turn you into JavaScript Expert.

6Q) What is BOM?

Ans: BOM stands for Browser Object Model. It provides interaction with the browser. The default object of the browser is the window.

7Q) What is DOM? What is the use of document object?

Ans:  DOM stands for Document Object Model. A document object represents the HTML document. It can be used to access and change the content of HTML.

8Q) What is the use of window object?

Ans:  The window object is automatically created by the browser that represents a window of a browser.

It is used to display the popup dialog box such as the alert dialog box, confirm dialog box, input dialog box, etc.

9Q) What is the use of history objects?

Ans:  The history object of the browser can be used to switch to history pages such as back and forward from the current page or another page. There are three methods of history object.

history.back()

history.forward()

history.go(number): number may be positive for forward, negative for backward.

10Q) How to write comments in JavaScript?

Ans: There are two types of comments in JavaScript.

Single Line Comment: It is represented by // (double forward slash)

Multi-Line Comment: It is represented by a slash with asterisk symbol as /* write comment here */

JavaScript Certification Questions and Answers

11Q) How to create a function in JavaScript?

Ans:  To create a function in JavaScript, follow the following syntax.

function function_name(){  

//function body

}

12Q) What are the JavaScript data types?

Ans: There are two types of data types in JavaScript:

Primitive Data Types

Non-primitive Data Types

13Q) What is the difference between == and ===?

Ans:  The == operator checks equality only whereas === checks equality and data type i.e. value must be of the same type.

14Q) How to write HTML code dynamically using JavaScript?

Ans: The innerHTML property is used to write the HTML code using JavaScript dynamically. Let's see a simple example:

document.getElementById('mylocation').innerHTML="<h2>This is heading using JavaScript</h2>";   

15Q) How to write normal text code using JavaScript dynamically?

Ans: The innerText property is used to write the simple text using JavaScript dynamically. Let's see a simple example:

document.getElementById('mylocation').innerText="This is text using JavaScript";   

16Q) How to create objects in JavaScript?

Ans:  There are 3 ways to create objects in JavaScript.

By object literal

By creating an instance of Object

By Object Constructor

Let's see a simple code to create an object using an object literal.

emp={id:102,name:"Rahul Kumar",salary:50000}   

Check Out Java Script Tutorial

17Q) How to create an array in JavaScript?

Ans: There are 3 ways to create an array in JavaScript.

By array literal

By creating an instance of Array

By using an Array constructor

Let's see a simple code to create an array using object literal.

var emp=["Shyam","Vimal","Ratan"];    

18Q) What does the isNaN() function?

Ans: The isNan() function returns true if the variable value is not a number.

19Q) What is the output of 10+20+"30" in JavaScript?

Ans: 3030 because 10+20 will be 30. If there is a numeric value before and after +, it is treated is binary + (arithmetic operator).

20Q) What is the output of "10"+20+30 in JavaScript?

Ans: 102030 because after a string all the + will be treated as a string concatenation operator (not binary +).

JavaScript Interview Questions for Experienced

21Q) Difference between Client-side JavaScript and Server-side JavaScript?

Ans:  Client-side JavaScript comprises the basic language and predefined objects which are relevant to running javascript in a browser. The client-side JavaScript is embedded directly in the HTML pages. This script is interpreted by the browser at run time.

Server-side JavaScript also resembles like client-side javascript. It has a relevant javascript which is to run in a server. The server-side JavaScript are deployed only after compilation.

22Q) In which location cookies are stored on the hard disk?

Ans: The storage of cookies on the hard disk depends on OS and the browser.

The Netscape navigator on Windows uses cookies.txt file that contains all the cookies. The path is : c:Program FilesNetscapeUsersusernamecookies.txt

The Internet Explorer stores the cookies on a file username@website.txt. The path is: c:WindowsCookiesusername@Website.txt.

23Q) What is the real name of JavaScript?

Ans:  The original name was Mocha, a name chosen by Marc Andreessen, founder of Netscape. In September of 1995, the name was changed to LiveScript. In December 1995, after receiving a trademark license from Sun, the name JavaScript was adopted.

24Q) What is the difference between undefined value and null value?

Ans: Undefined value: A value that is not defined and has no keyword is known as an undefined value. For example:

int number;//Here, the number has an undefined value.  

Null value: A value that is explicitly specified by the keyword "null" is known as null value. For example:

String str=null;//Here, str has a null value.  

25Q) How to set the cursor to wait in JavaScript?

Ans: The cursor can be set to wait in JavaScript by using the property "cursor". The following example illustrates the usage:

<script>  

window.document.body.style.cursor = "wait";

</script>

26Q) What is this?

Ans: var myArray = [[[]]];  

Three-dimensional array.

27Q) Are Java and JavaScript the same?

Ans: No, Java and JavaScript are the two different languages. Java is a robust, secured, and object-oriented programming language whereas JavaScript is a client-side scripting language with some limitations.

28Q) What is negative infinity?

Ans: Negative Infinity is a number in JavaScript which can be derived by dividing negative numbers by zero.

29Q) What is the difference between View state and Session state?

Ans: "View state" is specific to a page in a session whereas "Session state" is specific to a user or browser that can be accessed across all pages in the web application.

30Q) What are the pop-up boxes available in JavaScript?

Ans: Alert Box

Confirm Box

Prompt Box

31Q) How can we detect OS of the client machine using JavaScript?

Ans: The navigator.appVersion string can be used to detect the operating system on the client machine.

32Q) How to submit a form using JavaScript by clicking a link?

Ans: 

Let's see the JavaScript code to submit the form on clicking the link.

<form name="myform" action="index.php">  

Search: <input type='text' name='query' />

<a href="javascript: submitform()">Search</a>

</form>

<script type="text/javascript">

function submitform()

{

document.myform.submit();

}

</script>

33Q) Is JavaScript faster than ASP script?

Ans: 

Yes, because it doesn't require a web server's support for execution.

34Q) How to change the background color of HTML document using JavaScript?

Ans: 

     

<script type="text/javascript">  

document.body.bgColor="pink";

</script>

35Q) How to handle exceptions in JavaScript?

Ans:  By the help of try/catch block, we can handle exceptions in JavaScript. JavaScript supports try, catch, finally, and throw keywords for exception handling.

Our design of course tutorials and interview questions is practical and informative. At TekSlate, we offer resources to help you learn various IT courses. We avail of both written material and demo video tutorials. For in-depth knowledge and practical experience explore Online JavaScript Training.