Home >>Interview Questions >JavaScript Interview Questions

JavaScript Interview Questions

Interview Questions and answers in JavaScript

JavaScript can be complicated sometimes, in order to simplify the learning approach here are the top 60 JavaScript Interview Questions and Answers. Here are the frequently asked interview questions in the interview of JavaScript for professionals as well as beginners:

1. What is JavaScript?

Answer :JavaScript is basically a scripting language that is different from the Java language. JavaScript is object-based, lightweight, and cross-platform translated language. It is most frequently used for client-side validation. For a translation of the JavaScript code for the web browser, the JavaScript Translator is responsible that is embedded in the browser.

2. What are the features of JavaScript?

Answer :There are many features of the JavaScript language, here are some of the important ones:

  • It is lightweight.
  • It is interpreted programming language.
  • This language has been proven to be good for the applications that are network-centric.
  • JavaScript is complementary to Java.
  • It is also complementary to HTML.
  • It is an open source cross-platform.

3. What are the advantages of JavaScript?

Here are some of the advantages of JavaScript:

  • The server interaction in JavaScript is less.
  • The feedback to the user in this language is immediate.
  • The interactivity in this language is high.
  • The interfaces in JavaScript are richer.

4.What are the dis-advantages of JavaScript?

Answer:Here are some of the dis-advantages of JavaScript:

  • There is no support for multithreading
  • There is no support for multiprocessing
  • In JavaScript reading and writing of files is not allowed
  • There is no support for networking applications

5. Briefly describe a named function in JavaScript.

Answer :The function that has been named at the time of definition is known as a named function.
Let's take an example to understand this:

function msg()  
{  
  document.writeln("Named Function");  
}  
msg();  

6. What are the types of functions in JavaScript?

Answer:Here are the two types of function in JavaScript:

function display()  
{  
  document.writeln("Named Function");  
}  
display();  
var display=function()  
{  
 	 	document.writeln("Anonymous Function");  
}  
display(); 
  • Named:These types of functions have a name at the time of definition. Here is an example:
  • Anonymous:These are the functions that doesn't contain any name, the name is declared dynamically at the runtime. Here is an example:

7.How can you define anonymous function?

Answer:An anonymous function doesn't have any name. These types of functions are declared dynamically at the runtime with the help of function operator instead of the function declaration. The reason of using function operator is that it is more flexible than a function declaration. It can be easily replace an expression. Here is an example:

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

8. Is it okay to assign an anonymous function to a variable?

Answer:Indeed, you can assign an anonymous function to a variable.

9. What is an argument object in JavaScript?

Answer:An argument in JavaScript are passed to a function by the variables.

10. How to define closure?

Answer:In JavaScript, when a variable is defined outside the scope in reference, is accessed from some inner scope, then we need closures.
Here is an example:

var num = 10;  
function sum()   
{  
document.writeln(num+num);  
}   
sum();  

11. Which method is used to return the character from a specific index?

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

var str="Javatpoint";    
document.writeln(str.charAt(4));    

12. How to differentiate between JavaScript and Jscript?

Answer:The JavaScript language has been provided by Netscape. In order to avoid the trademark issue, Microsoft changed the name and called it Jscript. In other words, JScript is the same as JavaScript, but Microsoft provides it.

13. How can you write a hello word example of JavaScript?

Answer:Here is an example of JavaScript hello world is given below. Please note that you need to place it inside the body tag of HTML.

<script type="text/javascript">  
document.write("JavaScript Hello World!");  
</script>  

14. How you can use external JavaScript file?

Answer:Let's assume that js file name is message.js, enter the following script tag inside the head tag:

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

15. Do you think JavaScript is a case sensitive language?

Answer:Indeed, JavaScript is a case sensitive language. Here is an example:

Var msg = "JavaScript is a case-sensitive language"; //Here, var should be used to declare a variable  
function display()   
{  
document.writeln(msg); // It will not display the result.  
}   
display();  

16. What do you mean by BOM?

Answer:The full form of the abbreviation ‘BOM’ is Browser Object Model. It is known to provide interaction with the browser. Window is the default object of a browser hence, you can call all the functions of the window either by specifying the window or directly. Various properties are provided by the window object like document, history, screen, navigator, location, innerHeight, innerWidth,

17. What do you mean by DOM? Describe the use of the document object?

Answer:The full form of the abbreviation ‘DOM’ is document object model. It represents the HTML document and can be used to access and modify the content of HTML.

18. Where you can use window object?

Answer:The window object is generated automatically by the browser and represents the window of a browser. Please note that it is not an object of JavaScript instead it is a browser object. It is also used to display the popup dialog box.
Here is a brief description:

Method Description
alert() This method displays the alert box having the message with ok button.
confirm() This method displays the confirm dialog box having the message with ok and cancel button.
prompt() This method displays a dialog box to get input from the user.
prompt() This method displays a dialog box to get input from the user.
open() This method opens the new window.
close() This method closes the current window.
setTimeout() This method performs the action after specified time like calling function, evaluating expressions.

19. How can you use history object?

Answer:You can use the history object of a browser to switch to history pages such as back and forward from the current page or another page. There are basically three methods of history object:

  • history.back() - This object loads the previous page.
  • history.forward() - This object loads the next page.
  • history.go(number) -Here, the number may be positive for forward, negative for backward. This object loads the given page number.

20. What are the types of comments in JavaScript?

Answer:There are generally two types of comments in JavaScript:

  • Single Line Comment: This is represented by // (double forward slash)
  • Multi-Line Comment Comment: : It is represented with slash with asterisk symbol as /* write comment here */

21. How can you create a function in JavaScript?

Answer: In order to create a function in JavaScript please follow the below mentioned syntax:

function function_name()
{  
//function body  
}  

22. What are the datatypes in JavaScript?

Answer:Generally, there are two types of datatypes in JavaScript:

a. Primitive data types: here are some of the primitive data types which are as follows:

Data Type Description
String This data type represents a sequence of characters, e.g., "hi"
Number This data type represents numeric values, e.g., 500
Boolean This data type represents boolean value either false or true
Undefined This data type represents an undefined value
Null  
  This data type represents null, i.e., no value at all

b.Non-Primitive data types:Here are some of the non-primitive data types which are as follows:

Data Type Description
Object This data type represents an instance through which members can be accessed.
Array This data type represents a group of similar values
RegExp This data type represents regular expression

23. Differentiate between == and ===

Answer:The ‘==’ operator in JavaScript checks the equality and on the other hand ‘===’ operator checks the equality and the data type, i.e., a value must be of the same type.

24. How can you write HTML code dynamically using JavaScript?

Answer:In order to write the HTML code dynamically using JavaScript, InnerHTML property is used. Have a look at the example here to understand it more clearly:

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

25. How can you write normal text code dynamically using JavaScript?

Answer:In order to write the normal text code dynamically using JavaScript, InnerHTML property is used. Have a look at the example here to understand it more clearly:

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

26. How can objects be created in JavaScript?

Answer:Generally, there are three ways by which Objects can be created in JavaScript:

  • Using object literal
  • Using creating an instance of Object
  • Using Object Constructor

Here is a simple code to create an object using the object literal:

emp={id:105,name:"Sonu kumar",salary:150000} 

27. How can you create an array in JavaScript?

Answer:You can create an array in JavaScript using these three ways:

  • Using array literal
  • Instance of Array
  • Using an Array constructor

Here is a simple code to create an array in JavaScript:

var emp=["Sonu","mithun","PHPTPOINT"];

28. What is the isNaN() function?

Answer:Whenever the value of a variable is not a number, the isNaN() function returns true. Here is an example:

<script>
function number(num) 
{  
  if (isNaN(num)) 
  {  
    return "Not a Number";  
  }  
  return "Number";  
}  
console.log(number('1000F'));  
// expected output: "Not a Number"  
  
console.log(number('1000'));  
// expected output: "Number"  
</script>

29. Derive the output of 10+20+"30" in JavaScript?

The output of the code 10+20+"30" in JavaScript is 3030 as 10+20 will be 30. This is because, if there is any numeric value pre or post of + then it treats it as binary + (arithmetic operator).
Here is the code;

<script>
function display()  
{  
  document.writeln(10+20+"30");  
}  
display();  
</script>

30. Derive the output of "10" +20+30 in JavaScript?

The output of "10"+20+30 in JavaScript is 102030. This is because after a string all the + will be treated as string concatenation operator instead of (not binary +).

31. What is the difference between client side JavaScript and Server side JavaScript?

Answer:Client-side JavaScript - this basically constitutes the basic language and predefined objects that are relevant to running JavaScript in a browser. It is embedded directly in the HTML pages. This script is interpreted by the browser at runtime.

Server-side JavaScript - This resembles the client-side JavaScript. Server-side JavaScript has a relevant JavaScript which is to run in a server. Please note that only after compilation server-side JavaScript is deployed.

32.Do you know the location where cookies are stored in the hard disk?

Answer:It depends on the OS and the browser that where should be the storage of the cookies. Cookies.txt file that contains all the cookies is used on the Netscape Navigator on Windows. Here is the path c:\Program Files\Netscape\Users\username\cookies.txt [email protected] is the name of the file where the Internet Explorer stores the cookies. Here is the path: c:\Windows\Cookies\[email protected].

33. Do you know the real name of JavaScript?

Marc Andreessen, founder of Netscape had chosen the original name of JavaScript i.e. Mocha. The name was changed to LiveScript in the September of 1995. After receiving a trademark license from Sun in the December of 1995, the name JavaScript was adopted.

34. Differentiate between undefined value and null value.

Answer:Undefined value : A value without a keyword and that has not been defined is called as undefined value.
Here is an example:

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

Null Value:This is the value that is specified by the keyword “null”. Here is an example:

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

35. How can you set the cursor to wait in JavaScript?

Answer:With the help of property named as "cursor", you can set the cursor to wait in JavaScript. Here is an example depicting the usage:

<script>  
window.document.body.style.cursor = "wait";   
</script>  

No Sidebar ads