Temporal Dead Zone
The Temporal Dead Zone is the stretch of time between entering a scope and executing a let, const, or class declaration inside it. The name already exists during that window, but reading or writing it throws a ReferenceError.
Detailed Explanation
When the engine enters a block, it creates bindings for every declaration in that block before running a single statement. A var binding is created and immediately set to undefined. A let, const, or class binding is created uninitialized. Uninitialized is a real, observable runtime state, not a missing variable. Any access to the binding while it is in that state throws ReferenceError: Cannot access 'x' before initialization.
So there are three distinct situations, and the error messages tell them apart:
- The name was never declared anywhere:
ReferenceError: x is not defined. - The name is declared with
varand read early: no error, you getundefined. - The name is declared with
let/const/classand read early:ReferenceError: Cannot access 'x' before initialization.
The zone exists for two reasons. The first is const. A const must be assigned exactly once, at its declaration, so there is no sensible default value the engine could hand out beforehand — undefined would be a value the programmer never wrote. The second reason is diagnostics. Using a variable before declaring it is almost always a mistake, and var's silent undefined turns that mistake into a confusing downstream failure instead of a clear error at the guilty line.
The word "temporal" is doing real work. The zone is defined by execution time, not by position in the source. A function written above a const declaration can read that const perfectly well, as long as the function is called after the declaration has run. What matters is whether the declaration has executed by the moment of access, not whether it appears earlier in the file.
typeof is not an escape hatch. For undeclared names, typeof maybe safely returns "undefined". For a name sitting in its TDZ, typeof throws like any other access. So the old defensive idiom if (typeof config !== "undefined") is not safe against block-scoped declarations.
Two less obvious places where the zone bites. Default parameter values are evaluated left to right in their own scope, so function f(a = b, b = 2) throws when called as f() because b is still uninitialized. And a self-referencing initializer such as let n = n + 1 throws, because the n on the right-hand side resolves to the new, uninitialized binding rather than to anything outer.
Class declarations behave the same way, which is why new Point() before the class Point line fails while calling a hoisted function declaration before its line succeeds.
The cost is essentially zero. Engines track initialization with a flag and often prove statically that a variable is already initialized, eliminating the check. What the TDZ really costs you is the habit of declaring things before using them, which is a benefit disguised as a constraint.
Code Examples
Three different outcomes for "reading too early".
// Three different outcomes for "reading too early".
console.log(legacy); // => undefined (var: hoisted and initialized to undefined)
var legacy = 1;
try {
console.log(modern); // ReferenceError: Cannot access 'modern' before initialization
} catch (err) {
console.log(err.name); // => ReferenceError
}
let modern = 2;
try {
console.log(missing); // ReferenceError: missing is not defined
} catch (err) {
console.log(err.message); // => missing is not defined
}"Temporal", not "positional": the function is defined above, but called below.
// "Temporal", not "positional": the function is defined above, but called below.
function readLimit() {
return LIMIT; // resolves when called, not when defined
}
// readLimit(); // would throw: LIMIT is still uninitialized here
const LIMIT = 100;
console.log(readLimit()); // => 100 declaration has now executedtypeof does not protect you, and defaults can land in the zone too.
// typeof does not protect you, and defaults can land in the zone too.
try {
console.log(typeof rate); // throws, unlike typeof on an undeclared name
} catch (err) {
console.log(err.name); // => ReferenceError
}
let rate = 0.2;
function scale(value = factor, factor = 2) {
return value * factor;
}
console.log(scale(3, 4)); // => 12 both arguments supplied, no default evaluated
// scale(); // ReferenceError: Cannot access 'factor' before initializationCommon Gotchas
`typeof` throws inside the zone
`typeof x` returns `"undefined"` for a name that was never declared, but throws a `ReferenceError` for a `let` or `const` that has not been initialized yet. Feature-detection idioms built on `typeof` do not cover block-scoped variables.
`class` declarations are in the zone too
unlike function declarations, classes are hoisted but uninitialized, so instantiating a class above its definition fails. Define classes before the code that constructs them.
Self-reference in the initializer fails
`let count = count + 1` throws even when an outer `count` exists, because the inner binding already shadows it and is still uninitialized.
The error names the variable
`Cannot access 'total' before initialization` means TDZ, while `total is not defined` means no declaration exists in any reachable scope. The two messages point at completely different fixes.