Java Script Interview Questions and Answers

Difference between == and === in javascript?

The == checks value equal or not like
for example :

var a = 1;
var b = 1;
if(a == b){
    //true
}

from the above example a type is number and b type is also number.

var a = 1;
var b = '1';
if(a == b){
    //true
}

from the above example a is number and b is string. The type is different but the value is same.
Where as === check the value and type also.

var a = 1;
var b = '1';
if(a === b){}else{ // false}

the inner logic of === is like 

if(a == b && typeof(a) == typeof(b)){}

 

Difference between undefined and not defined in JavaScript?

In javascript when we are accessing/reading unknown variable (never declared before), then it will show error like Uncaught ReferenceError: xxx is not defined
where as undefined means, initilized a variable but not assigned a value like below

var a;
console.log(a); // undefined
and also 
var a = {name:"test"};
console.log(a.id) // undefined

from above both cases the result is undefined

What are the Data Types in  JavaScript ?

Below are the some data types in javascript
Number
String
Boolean
Object
Undefined

What is 'this' keyword in JavaScript?

'this' is refers to current execution object.

for examaple : 

var a = {
    fname : "John",
    lname : "Mike",
    fullname : function(){
        return this.fname + ' ' + this.lname;
    }
}
a.fullname() // output : John Mike

 

Global Variables Javascript ?

Global variables declared in outside of the function. We can access the variable any where in the page. Global variables have Global Scope.
For examaple :

var a = 10;
function b(){
    console.log(a); // output : 10
}

and also we can read the value using window object like below

console.log(window.a) // output : 10

 

Local Variables Javascript ?

Local Variables declared inside of the function, The scope will be within the function only. Local variables have functional scope.
For example :

function a(){
    var b = 0;
}
a();
console.log(b); // output : b is not defined

 

What is the output of the below code ?

var b = 10;
function a(){
    var b = 0;
}
a();
console.log(b);

 

The value of b is 10;
Because initial b is Global variable and the second b variable in the function, the second b variable have functional scope.
And one more thing in the function b variable is declared as a new variable. which will not override the global value.

What is "use strict" in javascript.

The "use strict" introduced in ECMAScript version 5. Which means the code is executed in strict mode. And "use strict" is declare in 1st line of the script or a function.
For examaple :

"use strict";
b = 10; // in this line it will return an error like b is not defined.

 

What is Lexical scope in Javascript.

Lexical scope nothing but nested group of functions, the inner functions have access parent function varibale.
For example :

functon a(){
    var d = 0;
    console.log(d); // output : 0
    console.log(e); // output : e is not defined
    function b(){
        var e = 10;
        console.log(d); // output : 0
        console.log(e); // output : 10
    }
}

 

Difference between const, let and var.

This is an advanced featuer from ES6. const, let and var are used for declaring a varibale only but they three have different scope.

const : When the value declared in const, the value never changed/modified.

const a = 10;
a = 15; // throw error.

let : When the value declared in let, It will have block scope.  Once a variable initilized with let, if we initilized with any type like let, var and const it will throw the error.

for(let i=0;i<3;i++){
    setTimeout(function(){
		console.log("i value is " + i);
	},100);
}

Output is :
i value is 0
i value is 1
i value is 2

var : When the value declared in var, It will have functional scope.

for(var i=0;i<3;i++){
    setTimeout(function(){
		console.log("i value is " + i);
	},100);
}


Output is :
i value is 3
i value is 3
i value is 3