Interview GURU

JavaScript interview questions

There are most popular JavaScript interview questions asked in many companies.

1. What is JavaScript?

JavaScript is a scripting language. It is different from Java language. It is object-based, lightweight, cross-platform translated language. It is widely used for client-side validation. The JavaScript Translator (embedded in the browser) is responsible for translating the JavaScript code for the web browser.

2. List some features of JavaScript.

Some of the features of JavaScript are:
  • Lightweight
  • Interpreted programming language
  • Good for the applications which are network-centric
  • Complementary to Java
  • Complementary to HTML
  • Open sourceCross-platform

3. List some of the advantages of JavaScript.

Some of the advantages of JavaScript are:
  • Server interaction is less
  • Feedback to the visitors is immediate
  • Interactivity is high
  • Interfaces are richer

4. List some of the disadvantages of JavaScript.

Some of the disadvantages of JavaScript are:
  • No support for multithreading
  • No support for multiprocessing
  • Reading and writing of files is not allowed
  • No support for networking applications.

5. Define a named function in JavaScript.

The function which has named at the time of definition is called a named function. For example
function test()  
  document.writeln("Named Function");  

6. Name the types of functions

The types of function are:
  • Named - These type of functions contains name at the time of definition. For Example:
function test()  
  document.writeln("Named Function");  

  • Anonymous - These type of functions doesn't contain any name. They are declared dynamically at runtime.
var test=function()  
  document.writeln("Anonymous Function");  

More details...

7. Define anonymous function

It is a function that has no name. These functions are declared dynamically at runtime using the function operator instead of the function declaration. The function operator is more flexible than a function declaration. It can be easily used in the place of an expression. For example:

var display=function()  
  alert("Anonymous Function is invoked");  

More details...

8. Can an anonymous function be assigned to a variable?

Yes, you can assign an anonymous function to a variable.

9. In JavaScript what is an argument object?

The variables of JavaScript represent the arguments that are passed to a function.

10. Define closure.

In JavaScript, we need closures when a variable which is defined outside the scope in reference is accessed from some inner scope.

var num = 10;  
function sum()   

More details...

11. If we want to return the character from a specific index which method is used?

The JavaScript string charAt() method is used to find out a char value present at the specified index. The index number starts from 0 and goes to n-1, where n is the length of the string. The index value can't be a negative, greater than or equal to the length of the string. For example:

var str="Javatpoint";    

More details...

12. What is the use of history object?

The history object of a 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.
  1. history.back() - It loads the previous page.
  2. history.forward() - It loads the next page.
  3. history.go(number) - The number may be positive for forward, negative for backward. It loads the given page number.

13. How to write a comment in JavaScript?

There are two types of comments in JavaScript.
  1. Single Line Comment: It is represented by // (double forward slash)
  2. Multi-Line Comment: Slash represents it with asterisk symbol as /* write comment here */

14. How to create a function in JavaScript?

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

function function_name(){  
//function body  

More details...

15. What is the difference between == and ===?

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

16. How to write HTML code dynamically using JavaScript?

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>";   

17. How to write normal text code using JavaScript dynamically?

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";   

18. How to create objects in JavaScript?

There are 3 ways to create an object in JavaScript.
  1. By object literal
  2. By creating an instance of Object
  3. By Object Constructor
Let's see a simple code to create an object using object literal.

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

More details...

19. How to create an array in JavaScript?

There are 3 ways to create an array in JavaScript.
  1. By array literal
  2. By creating an instance of Array
  3. By using an Array constructor
Let's see a simple code to create an array using object literal.

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

More details...

20. What does the isNaN() function?

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

function number(num) {  
  if (isNaN(num)) {  
    return "Not a Number";  
  return "Number";  
// expected output: "Not a Number"  
// expected output: "Number"  

More details...

21. What is the output of 10+20+"30" in JavaScript?

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

function test()  

More details...

22. What is the output of "10"+20+30 in JavaScript?

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

function test()  

More details...

23. Difference between Client side JavaScript and Server side JavaScript?

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 by in the HTML pages. The browser interprets this script at runtime.

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

24. In which location cookies are stored on the hard disk?

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

The Netscape Navigator on Windows uses a cookies.txt file that contains all the cookies. The path is c:\Program Files\Netscape\Users\username\cookies.txt

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

25. What is the difference between undefined value and null value?

Undefined value: A value that is not defined and has no keyword is known as undefined value. For example:
int number;//Here, a number has an undefined value.  

Null value: A value that is explicitly specified by the keyword "null" is known as a null value. For example:
String str=null;//Here, str has a null value.  

More details...

26. How to set the cursor to wait in JavaScript?

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

<script> = "wait";   

More details...

27. What is this [[[]]]?

This is a three-dimensional array.

var myArray = [[[]]];  

28. What is negative infinity?

Negative Infinity is a number in JavaScript which can be derived by dividing the negative number by zero. For example:

var num=-5;  
function display()  
//expected output: -Infinity  

More details...

29. What is the difference between View state and Session state?

"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.

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

  • Alert Box
  • Confirm Box
  • Prompt Box

More details...

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

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

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

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

<form name="myform" action="index.php">  
Search: <input type='text' name='query' /> 
<a href="javascript: submitform()">Search</a> 
<script type="text/javascript"> 
function submitform() 



More details...

33. How to change the background color of HTML document using JavaScript?

<script type="text/javascript">  

34. How to handle exceptions in JavaScript?

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

35. What is the requirement of debugging in JavaScript?

JavaScript didn't show any error message in a browser. However, these mistakes can affect the output. The best practice to find out the error is to debug the code. The code can be debugged easily by using web browsers like Google Chrome, Mozilla Firebox.

To perform debugging, we can use any of the following approaches:
  • Using console.log() method
  • Using debugger keyword

36. What is the use of debugger keyword in JavaScript?

JavaScript debugger keyword sets the breakpoint through the code itself. The debugger stops the execution of the program at the position it is applied. Now, we can start the flow of execution manually. If an exception occurs, the execution will stop again on that particular line.. For example:

function display()  
x = 10;    
y = 15;    
z = x + y;    

More details...

37. What is the role of a strict mode in JavaScript?

The JavaScript strict mode is used to generates silent errors. It provides "use strict"; expression to enable the strict mode. This expression can only be placed as the first statement in a script or a function. For example:

"use strict";    

38. What is the use of Math object in JavaScript?

The JavaScript math object provides several constants and methods to perform a mathematical operation. Unlike date object, it doesn't have constructors. For example:

function display()  

More details...

39. What is the use of a Date object in JavaScript?

The JavaScript date object can be used to get a year, month and day. You can display a timer on the webpage by the help of JavaScript date object.

function test()  
  var date=new Date();    
  var day=date.getDate();    
  var month=date.getMonth()+1;    
  var year=date.getFullYear();    
  document.write("<br>Date is: "+day+"/"+month+"/"+year);    

More details...

40. What is the use of a Number object in JavaScript?

The JavaScript number object enables you to represent a numeric value. It may be integer or floating-point. JavaScript number object follows the IEEE standard to represent the floating-point numbers.

function display()  
var x=102;//integer value    
var y=102.7;//floating point value    
var z=13e4;//exponent value, output: 130000    
var n=new Number(16);//integer value by number object    
document.write(x+" "+y+" "+z+" "+n);    

41. What is the use of a Boolean object in JavaScript?

The JavaScript Boolean is an object that represents value in two states: true or false. You can create the JavaScript Boolean object by Boolean() constructor.

function display()  


More details...

42. What is the use of a TypedArray object in JavaScript?

The JavaScript TypedArray object illustrates an array like a view of an underlying binary data buffer. There is any number of different global properties, whose values are TypedArray constructors for specific element types.

function test()  
var arr1= [1,2,3,4,5,6,7,8,9,10];     
       arr1.copyWithin(2) ;   

More details...

43. What is the use of a Set object in JavaScript?

The JavaScript Set object is used to store the elements with unique values. The values can be of any type i.e. whether primitive values or object references. For example:

function test()  

var set = new Set();   
for (let elements of set) {   

More details...

44. What is the use of a WeakSet object in JavaScript?

The JavaScript WeakSet object is the type of collection that allows us to store weakly held objects. Unlike Set, the WeakSet are the collections of objects only. It doesn't contain the arbitrary values. For example:

function display()  

var ws = new WeakSet();   
var obj1={};   
var obj2={};   
//Let's check whether the WeakSet object contains the added object   

More details...

45. What is the use of a Map object in JavaScript?

The JavaScript Map object is used to map keys to values. It stores each element as key-value pair. It operates the elements such as search, update and delete on the basis of specified key. For example:

function test()  

var map=new Map();   

More details...

46. What is the use of a WeakMap object in JavaScript?

The JavaScript WeakMap object is a type of collection which is almost similar to Map. It stores each element as a key-value pair where keys are weakly referenced. Here, the keys are objects and the values are arbitrary values. For example:

function display()  
var wm = new WeakMap();    
var obj1 = {};    
var obj2 = {};    
var obj3= {};    
wm.set(obj1, "jQuery");    
wm.set(obj2, "AngularJS");    

More details...