Javascript Interview questions
1) var vs let vs const
A variable declared using ‘var’ can be redefined and even redeclared anywhere throughout its scope.
var x = 30;
console.log(x); //prints 30
x = "Hi"; //redefining or re-assigning (works without any error)
console.log(x); //prints "Hi"
var y = 10;
console.log(y); //prints 10
var y = "Hello"; //Redeclaring (works without any error)
console.log(y) //Prints "Hello"
A variable declared using ‘let’ can be redefined within its scope but cannot be re-declared within its scope.
let x = 11;
console.log(x); //prints 11
x = "IB"; //works without any error
console.log(x); //prints "IB"
let y = 12;
console.log(y); //prints 12
let y = "Scaler"; // error: Identifier y has already been declared
let z = 13;
if(true){
let z = "Fun"; //works without any error as scope is different.
console.log(z) //prints "Fun"
}
console.log(z) //prints 13
A variable declared using ‘const’ cannot be redefined or re-declared within its scope.
const x = 10;
console.log(x); //prints 10
x = 11; // error: Assignment to constant variable.
const y;
y = 2; //error
const z = 12;
console.log(z) //prints 12
const z= 13; // error: Identifier 'y' has already been declared
2) Lexical Scope in JavaScript
Lexical scope is the ability for a function scope to
access variables from the parent scope. We call the child function to be
lexically bound by that of the parent function.
a and b is accessible (outer): 10 20
a and b and c is accessible (innner): 10 20 30
only a is accessible (global): 10
3) What Exactly Is a Temporal Dead Zone(TDZ) in JavaScript?
A temporal dead zone (TDZ) is the area of a block where a variable is inaccessible until the moment the computer completely initializes it with a value.
Example:
{
// bestFood’s TDZ starts here (at the beginning of this block’s local scope)
// bestFood’s TDZ continues here
// bestFood’s TDZ continues here
// bestFood’s TDZ continues here
console.log(bestFood); // returns ReferenceError because bestFood’s TDZ continues here
// bestFood’s TDZ continues here
// bestFood’s TDZ continues here
let bestFood = "Vegetable Fried Rice"; // bestFood’s TDZ ends here
// bestFood’s TDZ does not exist here
// bestFood’s TDZ does not exist here
// bestFood’s TDZ does not exist here
} Basically variable rest there, Out of three var, let and const, some of them rest there in temporal dead zone for a while, before they are initialized or declared.
4) Hoisting
Hoisting is a mechanism where variables and function declarations are moved to the top of their scope before code execution.
console.log(x); // prints undefined
var x = 100;
console.log(x); //prints 100
- Variables declared using var are hoisted to the top of their scope and initialized with a value of undefined(special type).
- Variables declared using let are hoisted to the top of their scope but are not initialized with any value.
- Variables declared using const are hoisted to the top of their scope but are not initialized with any value.
console.log(x); // prints undefined
var x = 100;
console.log(x); //prints 100
console.log(y); //Reference error
let y = 200;
console.log(y); //prints 200
console.log(z); //Reference error
const z = 300;
console.log(z); //prints 300
Currying function is configurable function
Comments
Post a Comment