Backbencher.dev

JS Daily 34 - Hoisting of Let Variables

Last updated on 24 Dec, 2020

What is the output of following code?

console.log(a);
let a = 10;
----o----

The output is:

ReferenceError: Cannot access 'a' before initialization

Variables declared using let are not hoisted. During the parsing step, JavaScript is aware about the variable. But, till line 2, it will keep the variable in Temporal Dead Zone. That means, we cannot use the code before the variable declaration, as mentioned in the error message.

--- ○ ---
Joby Joseph
Web Architect