Sub topic 6
The Temporal Dead Zone
This chapter assumes you know how let, const, and var are declared, how block scope works, and that declarations are hoisted (registered when a scope starts) before any code in that scope runs. The hoisting chapter showed that let throws an error where var gives undefined. This chapter explains the mechanism behind that error and the less obvious places where it shows up.
What the Temporal Dead Zone Is
When JavaScript enters a scope, it creates a binding for every let, const, and class declared in that scope. A var binding starts with the value undefined straight away. A let or const binding starts out uninitialized. Any attempt to read or write an uninitialized binding throws a ReferenceError.
The temporal dead zone (TDZ) is the stretch of time between entering the scope and running the declaration line. When the declaration runs, the binding gets its value: the initializer's value, or undefined for a plain let x;. From then on it behaves normally.
In V8 (Node.js and Chrome), the error message names the cause:
try {
console.log(total);
} catch (e) {
console.log(e.message); // "Cannot access 'total' before initialization"
}
let total = 100;Compare that with a name that was never declared, where the message is "total is not defined". The wording tells you which problem you have: a TDZ access or a missing declaration.
"Temporal" Means Time, Not Position
The TDZ depends on when code runs, not where it sits in the file. A function can mention a let variable declared below it, as long as the function is called after the declaration has run.
function read() {
return later; // fine, as long as read() runs after the declaration
}
try {
read(); // called while `later` is still in the TDZ
} catch (e) {
console.log(e.name); // 'ReferenceError'
}
let later = 'ready';
console.log(read()); // 'ready'The source code is the same both times read() is called. Only the timing changes.
Common Surprises
Shadowing an outer variable
A let inside a block or function shadows (hides) an outer variable with the same name for the whole scope, including the lines above the declaration. The inner binding is in its TDZ there, so JavaScript does not fall back to the outer one.
let value = 'outer';
function show() {
try {
console.log(value); // refers to the inner `value`, still in its TDZ
} catch (e) {
console.log(e.name); // 'ReferenceError'
}
let value = 'inner';
console.log(value); // 'inner'
}
show();This often happens during refactoring. You add let value = ... lower in a function, and earlier lines that used the outer value start throwing.
typeof is no longer safe
typeof on an undeclared name returns 'undefined' without throwing. typeof on a name in its TDZ still throws.
console.log(typeof neverDeclared); // 'undefined'
try {
console.log(typeof notYet);
} catch (e) {
console.log(e.name); // 'ReferenceError'
}
let notYet = 1;Self-reference in an initializer
The binding is still in its TDZ while its own initializer is being evaluated:
try {
const n = n + 1;
} catch (e) {
console.log(e.name); // 'ReferenceError'
}Default parameters
Parameters are initialized left to right, and each one has a TDZ until its turn comes. A default value can use an earlier parameter but not a later one.
function ok(a = 1, b = a + 1) {
return [a, b];
}
console.log(ok()); // [1, 2]
function broken(a = b, b = 1) {
return [a, b];
}
try {
broken();
} catch (e) {
console.log(e.name); // 'ReferenceError'
}Why the TDZ Is Useful
| Situation | `var` | `let` / `const` |
|---|---|---|
| Read before declaration | `undefined` | `ReferenceError` |
| `typeof` before declaration | `'undefined'` | `ReferenceError` |
| Read after declaration | value | value |
| Uninitialized declaration (`let x;`) | `undefined` | `undefined` (`const` requires an initializer) |
The simplest way to stay out of the TDZ is to declare variables at the top of the scope that uses them, before any code that reads them.
Exercise
What does this code print, and why? let label = 'global'; { console.log(label); let label = 'block'; }
Show answer
It prints nothing and throws a ReferenceError ("Cannot access 'label' before initialization"). The let inside the block creates a new binding called label for the whole block. That binding shadows the outer label, and at the console.log line it is still in its temporal dead zone, so JavaScript does not fall back to the outer 'global' value.
Exercise
Will calling init() below throw? function init() { return config.mode; } const config = { mode: 'dev' }; console.log(init());
Show answer
No, it prints 'dev'. The function body refers to config, but init() is only called after the const declaration has run, so the binding is already initialized. The TDZ depends on when the access happens, not on where the code appears in the file.