June 17, 2024

JavaScript Interview Questions with Answers

JavaScript is the most famous and easiest scripting language. It is widely used in web development. This article describes some common important interview questions and answers in JavaScript. Hope it will help you to build successful carrier.


What is JavaScript?
Java Script is a client side scripting language. It is Object Oriented Programming or OOP scripting language. Now all modern HTML pages are using JavaScript. It is also being used in game development, server-side programming, and desktop and mobile applications.

What do you know about the history of JavaScript?
At first JavaScript was developed by Brendan Eich at Netscape Communications Corporation in 1995. Today it is a trademark of Oracle Corporation. It is used under license for technology invented and implemented by Mozilla Foundation and Netscape Communications.

What is the latest version of JavaScript?
The latest version of JavaScript is: 1.8.5

Does Java and JavaScript are same?
The Java and JavaScript are not same. They have some difference. Some differences are given bellow:

  • Java is an OOP programming language but Java Script is an OOP scripting language.
  • Java can create applets or stand-alone applications but JavaScript can’t.
  • Java creates applications that run in a virtual machine or browser but JavaScript code is run on a browser only.
  • Java code needs to be compiled but JavaScript codes are all in text.
  • They require different plug-ins.

What is the difference between JavaScript and Jscript?
Both JavaScript and Jscript are scripting language and both are almost similar. JavaScript was developed by Netscape. Microsoft Corporation developed Jscript.

How can we add JavaScript onto a web page?
We can include JavaScript onto our page in several ways. Among them tow was are commonly used.

First way:
If the code is very short and only for single page, then we can place <script type=”text/javascript”> tag inside the <head> element. Sample code is given bellow:

    <title>Page Title</title>
    <script language="JavaScript" type="text/javascript">
        var name = "Mr. KKK"

Second way:
If the codes are very large, then we can make a JavaScript file, write down all the codes in that file. Finally include the path of that file.

    <title>Page Title</title>
    <script type="text/javascript" src="myjavascript.js"></script>

Is the JavaScript is case sensitive?
Yes, it is case sensitive. The function getElementById is not the same as getElementbyID.

What are the data types are used in JavaScript?
The data types of JavaScript are:
String, Number, Boolean, Function, Object, Null, Undefined.

What are the boolean operators in JavaScript?
And Operator: &&
Or Operator: ||
Not Operator: !

What is the difference between the operator “==” and “===”?
“==” checks only the equality,
“===” checks for equality and the types also.

Consider the following statements explain the output of the logs statements?

var p1 = 10;
var p2 = 10;
var p3 = new Number('10'); // A complex numeric object because new was used.
console.log(p1 === p2); 
console.log(p1 === p3);

var o1 = { same: 'same' };
var o2 = o1;

var ob1 = { same: 'same' };
var ob2 = { same: 'same' };

Corresponding outputs are given bellow:
console.log(p1 === p2); 	// Logs true.
console.log(p1 === p3); 	// Logs false because p3 contains a complex number object and p1 is a primitive value.
console.log(o1 === o2); 	// true
console.log(ob1 === ob2);	//false

What is ternary operator (? :)?
Ternary operator is the shortcut form of traditional if statement. This operator returns one of two values depending on the value of a Boolean expression. This operator takes three operands. The syntax is given bellow:

condition ? result1 : result2

Corresponding if-else statement is:

if (condition) 

This operator is right-associative. The expression a ? b : c ? d : e is written as a ? b : (c ? d : e) not as (a ? b : c) ? d : e. This operator cannot be overloaded.The ternary operator shouldn’t differ in performance from a well-written equivalent if-else statement. It reduces the codes. We can’t use ternary operator anywhere that applies to an if-else statement.

What are the loops available in JavaScript?
JavaScript supports the three type of loop:

  • for
  • while
  • do-while

Does JavaScript support foreach loop?
JavaScript 1.6(ECMAScript 5th Edition) support foreach loop

How we can make comments in JavaScript?
For line comments:
// is used for line comments
For block comments:

How to access the value of an html control in JavaScript?
We can access the value of a html control by using document.getElementById() method. Sample code to access a Textbox is given bellow:

var name = document.getElementById('txtMyName').value;

How to check the Checkbox status whether it is checked or not?
var status = document.getElementById(‘checkbox1’).checked;
It will return true or false.

How to create array in JavaScript?
We can create array in JavaScript in two ways:
First way:
var names = new Array();
Add Elements in Array:-
names[0] = “AAA”;
names[1] = “BBB”;
names[2] = “CCC”;
Second Way:
var names = new Array(“AAA”, “BBB”, “CCC”);

How to access the elements of an array?
In JavaScript the array index start with 0. The JavaScript code to print the second elements of an array:

Can you explain it?
var myArray = [[[]]];
This is a three dimensional array.

How can we submit a form in JavaScript?

What does the isNaN function do?
The isNaN function returns true if the argument is not a number.
document.write(isNaN(“Hello”)+ “<br>”);
document.write(isNaN(“2015/02/11”)+ “<br>”);
document.write(isNaN(456)+ “<br>”);

The corresponding output will be:

What is the output of “1”+2+3?
The output will be 123. Because the first value is a string type.

What is the output of 2+4+”5″?
The output will be 65. Because, first two value is integer, but last value is string.

What are the uses of Math Object in JavaScript?
Math objects are used for mathematical operations. Some examples are given bellow:
var x = Math.PI; // Returns the value of PI
var y = Math.sqrt(4); // Returns the square root of 4
var z = Math.sin(45); Returns the sine of 45

Give some methods names which are used to convert nonnumeric values into numbers?
Example is given bellow:
var a1 = Number(“Hello world!”); //NaN
var a2 = Number(NaN); //NaN
var a3 = Number(true); //1
var a4 = Number(“”); //0
var a5 = Number(“0010”); //10

Does JavaScript Support automatic type conversion?
Yes, JavaScript support automatic type conversion. Example is given bellow:
var a = ‘5’;
var b = a*1;
var c = +a;
typeof(a); //”string”
typeof(b); //”number”
typeof(c); //”number”

In order to JavaScript Inside which HTML element do we put the codes?
We will put the code inside <script> element.

Is it possible to split JavaScript code into several lines?
Yes, it is possible to break or split statement into several lines. We can do that by using a backslash [\] at the end of the first line.

Among JavaScript and ASP script, which one is faster?
JavaScript is faster than ASP script. Because JavaScript is a client-side language and ASP is a server-side language.

What is the negative infinity in JavaScript?
In JavaScript, it is a number which can be derived by dividing negative number by zero. It represents negative infinity. It is a special number which comes up when a negative number is divided by zero.

What is ‘this’ keyword in JavaScript?
In JavaScript ‘this’ keyword is used to point the current object in the code.

Explain the difference between ViewState and SessionState?
ViewState- in a session it is specific to a page
SessionState- in a session it is specific for a user that can be accessed across all the pages in the web application.

What is === operator in JavaScript?
In JavaScript === is a strict equality operator which returns true when the two operands are having the same value without any type conversion. It returns false if either the value or the type of the two variables are different.

How can we change the style/class of an element?
We can do this in the following ways:
document.getElementById(“myText”).style.fontSize = “10?;
document.getElementById(“myText”).className = “myclass”;

What is Variable typing in JavaScript?
In JavaScript when we assign multiple type values to a same variable then it is called variable typing.
For example consider the following codes where integer and stringy value is assigned to a same variable.
i = 20;
i = “cybarlab”;

What types of Pop up boxes available in JavaScript?
The list of pop up boxes available in JavaScript is given bellow:
Confirm and

What is the uses of Void(0) in JavaScript?
It is used to prevent the page from refreshing and parameter “zero” is passed while calling.
It is used to call another method without refreshing the page.

Why delete operator is used in JavaScript?
In JavaScript, the delete operator is used to delete all the variables used in the program. But it does not delete the variables declared with the VAR keyword.

What is the use of blur function in JavaScript?
In JavaScript the Blur function is used to remove the focus from the specified object.

Why Push method is used in JavaScript?
In JavaScript the push method is used to add or append one or more elements to the end of an Array. We can also append multiple elements by passing multiple arguments

Why unshift method is used in JavaScript?
In JavaScript the Unshift method is like push method which works at the beginning of the array.  It is used to prepend one or more elements to the beginning of the array.

What do you know about event bubbling and capturing in JavaScript?
In HTML the DOM elements can be nested inside each other. Event bubbling and event capturing are two ways of event propagation in the HTML DOM.
For event bubbling, the event of inner most elements is triggered first and then propagated to outer elements.
For event capturing, the event of outer most elements is triggered first and then propagated to inner elements.

<div class="d1">1        <!-- outer most -->
    <div class="d2">2
        <div class="d3">3 <!-- inner most -->

For example consider the above conditions.
For Bubbling:  3 -> 2 -> 1
For Capturing: 1 -> 2 -> 3

Rashedul Alam

I am a software engineer/architect, technology enthusiast, technology coach, blogger, travel photographer. I like to share my knowledge and technical stuff with others.

View all posts by Rashedul Alam →

One thought on “JavaScript Interview Questions with Answers

Leave a Reply

Your email address will not be published. Required fields are marked *