The Temporal Dead Zone (TDZ): A Complete Explanation
The TDZ is the gap between a scope's start and a let/const declaration line. Here is exactly what it is and the errors it causes.
The Temporal Dead Zone (TDZ): A Complete Explanation
The Temporal Dead Zone (TDZ) is the period between entering a scope where a let or const variable is declared, and reaching the actual declaration line. During this period, accessing the variable throws a ReferenceError.
What the TDZ Is
{ // x is in the TDZ here console.log(x); // ReferenceError: Cannot access 'x' before initialization let x = 5; // x is NOT in the TDZ here } `` The moment the engine enters the block, it allocates memory for `x` (so `x` is hoisted), but `x` is in the TDZ until the `let x = 5` line. Accessing it during the TDZ throws. ### Why `var` Does Not Have a TDZ `var` is initialized to `undefined` during the memory phase, so accessing it early returns `undefined` instead of throwing. `let` and `const` are not initialized until the declaration line, so the TDZ is the gap. ### TDZ Is Per-Variable ```js { console.log(a); // ReferenceError (a is in TDZ) let a = 1; console.log(b); // OK (b's line is above this point) let b = 2; } `` The TDZ ends for each variable when the engine reaches its declaration line, not when the block starts. ### TDZ Applies to `const` Too `const` has the same TDZ behavior as `let`, plus the rule that it must be initialized at declaration. ### TDZ in Default Parameters ```js function foo(a = b, b = 2) {} // ReferenceError: Cannot access 'b' before initialization foo(); `` Default parameters are evaluated left to right. `b` is in the TDZ when `a`'s default is evaluated. ### TDZ in Loops ```js for (let i = i; i < 5; i++) {} // ReferenceError `` `i` in the initializer is in the TDZ until the declaration completes. ### TDZ and `typeof` Even `typeof` cannot escape the TDZ: ```js console.log(typeof x); // ReferenceError let x; `` This is different from `typeof` on an undeclared variable, which returns `"undefined"` without throwing. ### Why the TDZ Exists To catch bugs. Pre-ES6, accessing a `var` early silently returned `undefined`, which caused subtle bugs. The TDZ forces declare-before-use, which is safer and surfaces mistakes early. ### The Takeaway The TDZ is the gap between entering a scope and reaching a `let`/`const` declaration line. Accessing the variable during this gap throws `ReferenceError: Cannot access 'x' before initialization`. It applies to `let`, `const`, default parameters, and `class`. It exists to force declare-before-use.
The period between entering a scope where a let or const variable is declared and reaching the declaration line. Accessing the variable during this period throws ReferenceError: Cannot access 'x' before initialization.
No. typeof on a let or const variable in the TDZ throws ReferenceError. This is different from typeof on an undeclared variable, which returns 'undefined' without throwing.
var is initialized to undefined during the memory phase, so it is usable early. let and const are not initialized until the declaration line, so the gap (TDZ) throws an error. This forces declare-before-use and catches bugs.
Yes. Default parameters are evaluated left to right. A parameter that references a later parameter throws ReferenceError because that later parameter is still in the TDZ.
To prevent bugs caused by accessing variables before declaration. Pre-ES6, var silently returned undefined in that case. The TDZ forces declare-before-use and surfaces the mistake as a clear ReferenceError.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

