JavaScript Interview Questions and Answer

JavaScript interview questions

These JavaScript questions have been designed for various interviews, competitive exams and entrance tests. We have covered questions on both basic and advanced concepts which will help you improve your skills to face interview questions on JavaScript.

Who are these JavaScript interview questions designed for?

All the Front End developers, UI/ UX developers and designers will find these questions extremely useful. All freshers, BCA, BE, BTech, MCA and college students wanting to make a career in front end designing will be highly benefitted by these questions.

JavaScript interview questions topics

This section covers JavaScript topics like - features of JavaScript, advantages of JavaScript, scopes of a variable in JavaScript etc.

1. What is JavaScript?

JavaScript is a scripting language with object-oriented capabilities that enables you to create interactive web pages. It is a client side scripting language that executes in the user's browser.

JavaScript can trap user-initiated events such as button clicks, screen touch, dragging screen etc. and can be used for various activities like validating data, displaying popup messages, handling different events of DOM elements etc.

JavaScript does not require expensive development tools; you just need a text editor to write JavaScript code and a browser to display your web page.

2. What is the difference between Java and JavaScript?

JavaScript is very different from language called Java.

The difference between Java and JavaScript are as follows:

Java is an object oriented programming language in which source code is compiled into bytecode and run on a virtual machine. JavaScript is an object based scripting language where source code is interpreted directly by a browser.

Java program has file extension as “.Java”. JavaScript file has file extension as “.js”.

Java applications can run in any virtual machine(JVM) or browser. JavaScript code runs on browser only.

You require entire Java Development Kit(JDK) to write a Java Program. You just require a text editor to write a JavaScript program.  

3. What are the features of JavaScript?

Features of JavaScript

  • JavaScript is a lightweight, object-based scripting language.
  • JavaScript offers user more control over the browser.
  • JavaScript is a client-side technology. It can perform basic calculations on the browser that helps saving server load and network traffic.
  • JavaScript validates user input for errors before sending the data over to the server.
  • JavaScript is a cross-platform scripting language. All browsers can interpret JavaScript, it solves the problem of compilation and compatibility.
  • JavaScript can also dynamically generate HTML content for the web.
  • JavaScript can detect the User’s Browser and OS information.
  • JavaScript is case sensitive.

4. What are the advantages of JavaScript?

Following are the advantages of using JavaScript:

  • JavaScript is a simple language to learn and implement.
  • JavaScript is a client side language. You can validate user input, perform basic calculations etc. on the browser that helps saving server load and network traffic.
  • JavaScript requires no compilation process. The browser interprets JavaScript as HTML tags.
  • JavaScript is platform independent language. All modern browsers can understand and interpreted JavaScript code.
  • JavaScript helps to provide rich interface to the websites such as Drag and drop components, sliders etc.
  • JavaScript provides all capabilities of a procedural language.

5. What are the scopes of a variable in JavaScript?

Scope refers to the visibility of variables. JavaScript has two scopes - global and local.

Global Variables - A global variable has global scope which means it can be accessible from anywhere in your code.

Local Variables - A local variable is one which is declared within a JavaScript function and it remains visible only within that function.

6. What is the purpose of ‘This’ operator in JavaScript?

'This' keyword refers to the object from where it was called.

7. What is the difference between the operators ‘==’ & ‘===‘?

== operator makes type correction and automatically converts one type into another and return value based upon content equality.

=== operator supports strict equality and only return true if both variables are of same type and also contain same value.

9. What do mean by NULL in Javascript?

‘NULL’ implies no value or no object. It can be assigned to a variable as a representation of no value.

10. What is the difference between undeclared and undefined?

Undeclared variables are those that are not initialized or declared. If you try to get the value of an undeclared variable, then a runtime error is encountered.

Undefined variables are those that are declared in the program but have not been assigned with any value. If you try to get the value of an undefined variable, an undefined value is returned.

11. What is the difference between innerHTML and innerText?

innerHTML- It interprets an HTML tag if found in a string as HTML and sets the same content in HTML format.

innerText - It interprets an HTML tag as text and sets the content of the tag as plain text.

12. What is an event bubbling in JavaScript?

Event Bubbling is a technique in which the event is first captured and handled by the target element and then propagated to parent elements until it reaches the document object.


<body onclick="alert('body')">
  <div onclick="alert('div')">
    <span onclick="alert('span')">span</span>

In the above example, when a click event happens on <span>, it first runs the handlers on <span> followed by on the outer <div> and then on the outer <body>.

13. What is NaN in JavaScript?

NaN is a special value that denotes Not-a-Number and cannot be represented as a meaningful number. There are many ways in which NaN can happen such as dividing zero by zero, converting a non-numeric string into a number etc.

We can use isNan() function to see if a value is an NaN value. The function returns true if the argument passed is not a number otherwise it returns false.

14. What is break and continue statements?

Break statement is used to jump out of current loop without executing loop statement(s) below it.

Continue statement skips the rest of the loop statement(s) and continues with the next iteration in the loop.

15. What is the use of typeof operator?

typeof is an operator that returns string description of the operand indicating it's data type.


typeof ""         // Returns "string"
typeof 123               // Returns "number"

16. What are the different types of errors in JavaScript?

There are three types of errors:

Syntax Errors: These errors are errors in the syntax of the source code of a program. They occur at interpret time in JavaScript.

Run-time errors: These errors occur during execution while the program is running. For example, division of a number by zero, calling invalid functions etc.

Logical error: There errors occur when you make a mistake in the logic. Your program compiles and runs without error but doesn't produce intended result. They are hard to determine and debug as they tend to be hidden in the source code.

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

JavaScript is ‘not very strict’ in throwing errors. Sometimes, it displays result even when there are some errors. To overcome this problem we can use JavaScript strict mode. In ‘Strict mode’ JavaScript code throws all types of errors. This reduces bugs and makes debugging easier and thus, helps developers to avoid unnecessary mistakes.

To enable strict mode, write "use strict" at the top of the script.

18. Can you access Cookie using javascript?

Yes, we can access cookie using javascript. Cookies can be created, read and erased by JavaScript. They are accessible through the property document.cookie.

19. What is the difference between ‘null’ and ‘undefined’?

Both the keywords signify absence of value.

The differences are:

'null' is a special value represents 'no value'. 'null' can be assigned to a variable that signifies that we have defined a variable but have not assigned any value yet.

‘undefined’ means a variable has been declared but not defined yet. JavaScript assigns ‘undefined’ to any object that has been declared but not initialized or defined.