[2021] 18 Important Javascript Interview Questions

Javascript Interview Questions:

1. Difference between == and === ?

== compares only the value.

=== compares the value along the data type or type of the variable.
Eg:

1 == “1” => true;because both has the value 1.
1 === “1” => false;because one is integer and other one is string. So even though it has the same value, because of different data types it returns false.

Note: === can be used to compare with null/undefined.

2.Difference between null and undefined ?

null is a special object, since it returns the type as “object” and undefined means that variable has not been declared/not been assigned with any value.

3. How do you check a variable is undefined or not ?

Undefined variable can be checked using === with typeof keyword,

typeof var === ‘undefined’

4.When/why to use typeof ?

typeof is used to avoid ReferenceError.

if(myVar === undefined) // throws ReferenceError.
This can be avoided by using typeof

if(typeof myVar === undefined) // now no error.

5. What will be the output of the below program ?

function hello(){
   var a =b=3;
   alert(a+b);
}

Output:

6

6. What is the use of “use strict” ?

“use strict” can be used to avoid the new variable creations/declarations by mistake.

function hello(){
   myVariable = 10;
   alert(myVariable);
}

This will execute and return the output as “10”.
but

function hello(){
   "use strict";
   myVariable = 10;
   alert(myVariable);
}

Throws “Uncaught ReferenceError: myVariable is not defined”

this can be resolved by creating the variable with var keyword, so use strict allows to create the variable only with var, because if you type any variable wrongly then it will not allow you to create that as new variable.

function hello(){
   "use strict";
   var myVariable = 10;
   alert(myVariable);
}

7. Infinity or NaN[not a number] ?

function hello(){
   alert("js"/0);
}

This throws NaN[Not a Number] only, because “js” is not a number. If this is a number then you will get Infinity in the alert.

function hello(){
   alert(10/0);
}

8. What is the difference between setTimeout() and setInterval() ?

Both takes two arguments function and delay

setTimeout(func, delay) => The mentioned function will be called after the mentioned time delay.

setInterval(func, delay) => The mentioned function will be called repeatedly with the given time delay till this is cancelled.

9. How to stop setInterval() ?

var repeatInt = setInterval(fname, 500);
/* when you want to cancel */
clearInterval(repeatInt);

10. Can we assign number/string to the same variable ?

Yes.

function hello(){
   var i =10;
   alert(i);
   i = "Js";
   alert(i);
}

Alerts both 10 and “Js”. This we also call as “variable typing“.

11. Difference between “Undefined” and “not defined”?

function hello(){
   var i;
   alert(i);
}

Alerts “Undefined”. Because no value assigned to variable i.

function hello(){
   i;
   alert(i);
}

ReferenceError: i is not defined

Here i is not defined as variable. If you remove that line itself also returns same error.

12. How do you avoid refreshing ?

void(0) can be used to avoid the page from refreshing.

13. How to confirm whether a particular ID is exist or not ?

Confirming the existence of the ID will help us to resolve reference null/undefined errors and can be done like this,

var elem = document.getElementById('ID_OF_THE_ELEMENT');
if (elem === null) {
   alert('Element with this ID Does not exist!');
}else{
   alert('Element with this ID exist!');
}

14. What is Closure Function ?

A Function inside an another function is called closure function.

Example and the real use case is explained nicely here.

15. Different variable scopes and it’s sample use cases (var/let/default keyword difference and variable scopes) ?

16. What is event bubbling ?

17. What is Pure and Impure functions ?

18. What is call and apply ?

Leave a Reply