We are familiar with variable hoisting in JavaScript. If we try to access a variable (declared using var
keyword) before assign it any value, we get undefined
. But that's not the case with variables defined using let
or const
. If we try to access a variable (declared using let
or const
) before assigning it any value, we get ReferenceError
.
Many people believe the reason behind this behavior is that let
and const
are not hoisted, which is not true. Just like var
, they are hoisted, but what they lack is the initialization process which var
goes through but let
and const
do not.
For let
and const
, initialization is only complete when a value is assigned to the variable; and the period from start of the block scope to initialization of value, where we receive ReferenceError
, is known as the Temporal Dead Zone.
Wrapping up: All var
, let
and const
are hoisted. But, unlike var
, let
and const
do not go though initialization process. The time (zone) from start of block scope to actual initialization is called Temporal Dead Zone 🐱👤.
Top comments (0)