JavaScript Hoisting and the Temporal Dead Zone
Quick answer
Hoisting is an informal description of declaration behavior, not a source-code transformation. Different declarations create and initialize bindings at different times. A lexical binding can exist before it is readable.
How it works
An ordinary function declaration is initialized before statements in the surrounding ordinary function body execute. A var binding is initialized to undefined before the body runs. Let and const are created but stay uninitialized until their declaration is evaluated. Access during this interval, called the temporal dead zone (TDZ), throws ReferenceError. The same rule applies to typeof when it resolves an uninitialized binding.
function run() { console.log(double(5)); function double(n) { return n * 2; }}run();Expected output in Node.js:
10The function declaration is initialized before run executes its statements.
function inspect() { console.log(amount); var amount = 20; console.log(amount);}inspect();Expected output in Node.js:
undefined20The assignment of 20 happens at the declaration statement.
Common mistakes
The intentionally failing snippet { console.log(rate); let rate = 5; } throws ReferenceError, even if an outer rate exists. A later lesson teaches error handling; for now read the error and reorder the declaration before the use.
Practice
Manually trace creation, initialization and first read for var, let, const and an ordinary function declaration. Run the two working examples separately.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Hoisting and the Temporal Dead Zone — Knowledge Check
Apply the taught concept and reason about the result.
Which statement best describes hoisting?
What does function f(){ return g(); function g(){ return 4; } } console.log(f()); print?
What happens in { console.log(x); let x = 1; }?
What is the initial value of a function-scoped var before its initializer executes?
What is the simplest repair when a lexical name is read too early?