The temporal dead zone
typeof is safe on names that don't exist at all, but even typeof throws for a name in its TDZ.
Before running a scope, JavaScript first registers every declaration in it: function declarations arrive complete, var variables start as undefined, and let, const and class exist but can't be touched until their line runs; that forbidden stretch is the temporal dead zone.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Predict the output: console.log(a); var a = 5; console.log(a);
Fix this code without removing any line, only by moving one: console.log(double(4)); const double = n => n * 2;
Write a short example showing that a function written above a let can still use it when called afterwards.
Explain it without notes
What is hoisting? Describe what happens to function declarations, var, and let/const.
What is the temporal dead zone, and why was it designed this way?
Why does calling a function expression stored in var before its line give a TypeError, while one stored in const gives a ReferenceError?
Why does let x = 1; { console.log(x); let x = 2; } throw?
Console
(nothing was printed)Expected console output
level is 3
ReferenceError: Cannot access 'item' before initialization
ReferenceError: Cannot access 'later' before initialization
ReferenceError: Cannot access 'sayBye' before initialization
bye