Java Script Interview Questions

Q1. What is JavaScript?


JavaScript is a client-side as well as server side scripting language that can be inserted into HTML pages and is understood by web browsers. JavaScript is also an Object based Programming language

Q2. differences between Java and JavaScript?


Java is a complete programming language. In contrast, JavaScript is a coded program that can be introduced to HTML pages. These two languages are not at all inter-dependent and are designed for the different intent.  Java is an object – oriented programming (OOPS) or structured programming language like C++ or C whereas JavaScript is a client-side scripting language.

Q3. What is DOM? What is the use of document object?


DOM stands for Document Object Model. A document object represent the html document. It can be used to access and change the content of html.

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


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

Q5. What are undeclared and undefined variables?


Undeclared variables are those that do not exist in a program and are not declared. If the program tries to read 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 given any value. If the program tries to read the value of an undefined variable, an undefined value is returned.


Q6. What are global variables? How are these variable declared?


Global variables are those that are available throughout the length of the code, that is, these have no scope. The var keyword is used to declare a local variable or object. If the var keyword is omitted, a global variable is declared.


// Declare a global globalVariable = “Test”;

The problems that are faced by using global variables are the clash of variable names of local and global scope. Also, it is difficult to debug and test the code that relies on global variables.


Q7. What is the use of Void(0)?


Void(0) is used to prevent the page from refreshing and parameter “zero” is passed while calling.

Void(0) is used to call another method without refreshing the page.

Q8. What is the difference between .call() and .apply()?


The function .call() and .apply() are very similar in their usage except a little difference. .call() is used when the number of the function’s arguments are known to the programmer, as they have to be mentioned as arguments in the call statement. On the other hand, .apply() is used when the number is not known. The function .apply() expects the argument to be an array.

The basic difference between .call() and .apply() is in the way arguments are passed to the function. Their usage can be illustrated by the given example.

Q9. What is a “closure” in JavaScript? Explain with an example.


A closure is a function defined inside other function(called the parent function), and has the access to variables that are declared and defined in the parent function scope

The closure has access to variables in the three scopes:

Variables declared in their own scope

 1. Variables declared in their own scope.

 1. Variables declared in a parent function scope.

 2. Variables declared in the global namspace.


var globalVar = "abc";

// Parent self invoking function
(function function1 (function1Arg) { // begin of scope outerFunction
// Variable declared in outerFunction function scope
var function1Var = 'x';
// Closure self-invoking function
(function function2 (function2Arg) { // begin of scope innerFunction
// variable declared in innerFunction function scope
var function2Var = "y";
"function1Arg = " + function1Arg + "\n" +
"function1Var = " + function1Var + "\n" +
"function2Arg = " + function2Arg + "\n" +
"function2Var = " + function2Var + "\n" +
"globalVar = " + globalVar);
}// end of scope innerFunction)(5); // Pass 5 as parameter
}// end of scope outerFunction )(7); // Pass 7 as parameter


 function2 is closure that is defined insode function1 and has access to all varables declared and defined in the function1 scope. In addition, the function defined inside another function as a closure will have access to variables declared in the global namespace. 

Thus the output of the code will be:

function1Arg = 7

function1Var = x

function2Arg = 5

function2Var = y

globalVal = abc

Q10. What is the significance of including 'use strict' at the beginning of a JavaScript file? Explain its benefits.


The short and most important answer here is that use strict is a way to voluntarily enforce stricter parsing and error handling on your JavaScript code at runtime. Code errors that would otherwise have been ignored or would have failed silently will now generate errors or throw exceptions. In general, it is a good practice.

Some of the key benefits of strict mode include:

  • Makes debugging easier. Code errors that would otherwise have been ignored or would have failed silently will now generate errors or throw exceptions, alerting you sooner to problems in your code and directing you more quickly to their source.
  • Prevents accidental globals. Without strict mode, assigning a value to an undeclared variable automatically creates a global variable with that name. This is one of the most common errors in JavaScript. In strict mode, attempting to do so throws an error.
  • Eliminates this coercion. Without strict mode, a reference to a this value of null or undefined is automatically coerced to the global. This can cause many headfakes and pull-out-your-hair kind of bugs. In strict mode, referencing a a this value of null or undefined throws an error.
  • Disallows duplicate parameter values. Strict mode throws an error when it detects a duplicate named argument for a function (e.g., function foo(val1, val2, val1){}), thereby catching what is almost certainly a bug in your code that you might otherwise have wasted lots of time tracking down.
    • Note: It used to be (in ECMAScript 5) that strict mode would disallow duplicate property names (e.g. var object = {foo: "bar", foo: "baz"};) but as of ECMAScript 2015 this is no longer the case.
  • Makes eval() safer. There are some differences in the way eval() behaves in strict mode and in non-strict mode. Most significantly, in strict mode, variables and functions declared inside of an eval() statement are not created in the containing scope (they are created in the containing scope in non-strict mode, which can also be a common source of problems).
  • Throws error on invalid usage of delete. The delete operator (used to remove properties from objects) cannot be used on non-configurable properties of the object. Non-strict code will fail silently when an attempt is made to delete a non-configurable property, whereas strict mode will throw an error in such a case.