Sub topic 3
The Temporal Dead Zone
This chapter assumes you already know how the scope chain resolves names and how var is hoisted with an initial value of undefined. It covers what happens with let, const, and class before their declaration runs, and then how closures, loops, strict mode, and modules depend on those rules.
The Temporal Dead Zone
let, const, and class declarations are hoisted to the top of their block, but they are not initialized. The temporal dead zone (TDZ) is the stretch of time from the start of the scope until the declaration line executes. If you read the name during that time, JavaScript throws a ReferenceError.
function demo() {
try {
console.log(y);
} catch (e) {
console.log(e.name); // "ReferenceError"
}
let y = 1;
console.log(y); // 1
}
demo();typeof does not protect you here. It returns "undefined" for names that were never declared, but it still throws on a binding that is in its TDZ.
console.log(typeof notDeclaredAnywhere); // "undefined"
try {
console.log(typeof z);
} catch (e) {
console.log(e.name); // "ReferenceError"
}
const z = 0;Closures: Functions That Remember Their Scope
A closure is a function bundled with the scope it was created in. The function keeps a live reference to the variables in that scope, not a copy of their values.
function makeCounter() {
let count = 0;
return function () {
count += 1;
return count;
};
}
const next = makeCounter();
console.log(next()); // 1
console.log(next()); // 2The Classic Loop Closure Bug
Closures capture variables, so the scope of the loop variable decides what each closure sees. A var loop has one function-scoped i that every closure shares. A let loop creates a fresh binding for each iteration.
const withVar = [];
for (var i = 0; i < 3; i++) {
withVar.push(() => i);
}
console.log(withVar.map(f => f())); // [3, 3, 3]
const withLet = [];
for (let j = 0; j < 3; j++) {
withLet.push(() => j);
}
console.log(withLet.map(f => f())); // [0, 1, 2]Accidental Globals and Strict Mode
In sloppy mode, assigning to an undeclared name creates a property on the global object. Strict mode turns that mistake into a ReferenceError.
function leakSloppy() {
leaked = 1; // no declaration
}
leakSloppy();
console.log(globalThis.leaked); // 1
function leakStrict() {
"use strict";
try {
alsoLeaked = 1;
} catch (e) {
console.log(e.name); // "ReferenceError"
}
}
leakStrict();Module Scope in ES Modules
Each ES module has its own top-level scope. A top-level var in a module stays private to that module and does not become a property of globalThis. Modules also run in strict mode automatically, so accidental globals throw.
// Save as scope.mjs and run: node scope.mjs
var hidden = 1;
console.log(hidden); // 1
console.log(globalThis.hidden); // undefinedExercise
What does this code log, and why? let total = 10; function f() { console.log(total); let total = 20; } f();
Show answer
It throws a ReferenceError. The inner let total is hoisted to the top of f and shadows the outer total, and the inner binding is still in its temporal dead zone when console.log runs.
Exercise
A loop for (var k = 0; k < 3; k++) setTimeout(() => console.log(k), 0); logs 3 three times. What is the smallest change that makes it log 0, 1, 2?
Show answer
Change var to let. Each iteration then gets its own binding of k, and each callback closes over a different one.