DEV Community

Discussion on: The Secret of Hoisting in JavaScript

Collapse
 
bhupendra1011 profile image
bhupendra • Edited

variables declared using let and const are also hoisted but only difference is that in case of let/const , variables are not initialized with undefined as in case of var/function.

x=10;
console.log(x);
let x = 20; 
Enter fullscreen mode Exit fullscreen mode

above code will throw reference error as variable x in temporal dead zone . If it was not hoisted then x would have been 10.

Collapse
 
godcrampy profile image
Sahil Bondre

Yes you are correct indeed. I've fixed my error. Thank You for pointing this major blunder out!

Collapse
 
sajwan05 profile image
Sajwan05

Basically for let and const TDZ start with commencing of the block inside which particular variable is declared and initialized and it will exit TDZ when js interpreter/executor reach that line.