Sobes.tech
Middle+

Do let and const (hoisting) pop up? What happens when you access them before declaration?

sobes.tech AI

Answer from AI

Variables declared with let and const are technically hoisted, meaning their declarations are moved to the top of the block, but they are in the so-called "Temporal Dead Zone" (TDZ). This means that accessing them before their declaration line will result in a ReferenceError.

Example:

console.log(a); // ReferenceError: Cannot access 'a' before initialization
let a = 10;

console.log(b); // ReferenceError: Cannot access 'b' before initialization
const b = 20;

Thus, let and const are not accessible before their explicit declaration in the code, unlike var, which is initialized with undefined during hoisting and does not cause an error when accessed before declaration.