what is the scope of variable
ES5 has function scope & that is because of “hoisting”
ES6 doesn’t have hoisting ,it has block scope
if you declare variable with “var” keyword then “hoisting” will be there
for e p
console.log(x)//undefined
var x = 1;
js compiler will hoist the variable declaration,so the output is undefined,and in fact this section of code will just like this
var x;
console.log(x);
x = 1;
it’s called variable hoisting
block scope is introduced from ES6 onwards
for e,g
let x = 9
{
let x = 8;
console.log(x);//8
}
console.log(x);//9
javascript compiler doesn’t hoist variables declared with “let” keyword
for e p
console.log(x)
let x = 1;
it will throw an errror that is cannot access x before initalization
1. what is hoisting in javasrcipt
A process which is happening behind the scene,internally it is bring the declarations on top
2.How does block scope work
It’s nothing but the brackets {},an if condition,for loop,do while loop etc,
that is a block created which creates separate scope for declarations existing in that block
3.What is scope of a variable
Its’s the availability or visibility or lifetime of a variable