I think I've just started to understand the concept of the temporal dead zone, so I just wanted to confirm if I'm thinking in the right direction and if my understanding of this concept is correct.
So a temporal dead zone is closely related to hoisting in JavaScript. As opposed to common misunderstanding, variable declared with let
and const
are, in fact, hoisted to the top of their scope just like the var
variables, but they're left uninitialized. Hence, the ReferenceError when we try to access them before they're officially declared.
This period of time starting from when the let
/const
variables have been hoisted to the time when they're officially declared is the TDZ, according to my understanding.
Now, I'm confused about if the TDZ ends when we declare the variable, or when we initialize it? It should end when the variable is declared according to me, since it's given a value of undefined and there's no longer any error. Here's the code for a better understanding of what I'm talking about
console.log(someVariable); //ReferenceError
let someVariable;
console.log(someVariable) //undefined
Please correct me if I'm wrong anywhere.