Sub topic 10
The Temporal Dead Zone
This chapter assumes you know how var hoisting works and that each scope has a creation phase followed by an execution phase. You should also know that let and const are block-scoped.
Hoisted but Not Initialized
During the creation phase, JavaScript registers every declaration in a scope. A var binding is created and set to undefined immediately. A let, const, or class binding is also created at that point, so it is hoisted, but it stays uninitialized. Any attempt to read or write it throws a ReferenceError. The binding becomes usable only when execution reaches its declaration line.
The temporal dead zone (TDZ) is the stretch of time between the start of the scope and the moment the declaration runs.
function demo() {
try {
console.log(y);
} catch (e) {
console.log(e.name); // "ReferenceError"
}
let y = 1;
console.log(y); // 1
}
demo();"Temporal" Means Time, Not Position
The zone 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 show() {
return message; // refers to the outer let
}
let message = "ready";
console.log(show()); // "ready": called after initializationIf you moved console.log(show()) above the let line, the same function would throw, because the call would happen inside the TDZ.
Common Errors
TDZ Hides an Outer Variable
A block-level declaration shadows the outer name for the whole block, including the lines above the declaration. Developers often expect the lookup to fall back to the outer variable. It does not.
let color = "red";
{
try {
console.log(color); // inner color is in its TDZ
} catch (e) {
console.log(e.name); // "ReferenceError"
}
let color = "blue";
console.log(color); // "blue"
}typeof Is Not a Safe Check
typeof returns "undefined" for names that were never declared. A name in its TDZ has been declared, so typeof throws anyway.
console.log(typeof notDeclaredAnywhere); // "undefined"
try {
console.log(typeof z);
} catch (e) {
console.log(e.name); // "ReferenceError"
}
const z = 0;Self-Reference in the Initializer
The binding is still uninitialized while its own initializer is evaluated.
try {
const total = total + 1;
} catch (e) {
console.log(e.name); // "ReferenceError"
}Classes Used Too Early
Class declarations follow the same rule. Unlike function declarations, a class cannot be used before its declaration line.
try {
new Widget();
} catch (e) {
console.log(e.name); // "ReferenceError"
}
class Widget {}
console.log(typeof new Widget()); // "object"Comparison
| Declaration | Hoisted | Initial state | Access before declaration |
|---|---|---|---|
| `var` | Yes | `undefined` | Returns `undefined` |
| `let` | Yes | Uninitialized | `ReferenceError` |
| `const` | Yes | Uninitialized | `ReferenceError` |
| `class` | Yes | Uninitialized | `ReferenceError` |
| `function` declaration | Yes | Full function | Works |
Exercise
What does this code print, and why? let n = 10; function check() { console.log(n); let n = 20; } check();
Show answer
It throws a ReferenceError. The let n inside check shadows the outer n for the whole function body. When console.log(n) runs, the inner n is still in its temporal dead zone, so the lookup does not fall back to the outer value of 10.
Exercise
Does this code throw? const getLimit = () => limit; const limit = 5; console.log(getLimit());
Show answer
No, it prints 5. The arrow function only reads limit when it is called. By then the const limit = 5 line has run and the binding is initialized.