JavaScriptFundamental (ES6), part 1
Temporal dead zone in JavaScript
2 The temporal dead zone
fairRead a let before the line that declares it, then read a var the same way, and record what each one does. Then check what happens when a let shadows an outer name of the same spelling.
Stuck? Three hints, smallest first
- 1. A
letorconstbinding exists from the top of its block but cannot be read until its line runs. That gap is the temporal dead zone. - 2.
varis initialised toundefinedat the top of the function instead, which is why it reads asundefinedrather than throwing. - 3. Shadowing happens at parse time, not at the line. The inner name covers the whole block, including the part above its declaration.
function readLetTooEarly() {
const seen = [];
try { seen.push(price); } catch (e) { seen.push(e.constructor.name); }
let price = 500;
seen.push(price);
return seen;
}
function readVarTooEarly() {
const seen = [];
seen.push(typeof price);
var price = 500;
seen.push(typeof price);
return seen;
}
function typeofInTheDeadZone() {
let result;
try { result = typeof amount; } catch (e) { result = e.constructor.name; }
let amount = 1;
return result;
}
const outerRate = 'outer';
function shadowedInTheDeadZone() {
let seen;
try { seen = outerRate; } catch (e) { seen = e.constructor.name; }
let outerRate = 'inner';
return seen;
}
function callsBeforeDefined() {
return double(4);
function double(n) { return n * 2; }
}
console.log('let read too early :', readLetTooEarly());
console.log('var read too early :', readVarTooEarly());
console.log('typeof in the gap :', typeofInTheDeadZone(), '<- typeof does not save you here');
console.log('shadowed outer name:', shadowedInTheDeadZone());
console.log('function hoisting :', callsBeforeDefined());
Output
7 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 7 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('reading a let above its line throws', readLetTooEarly()[0], 'ReferenceError');
test('below the line it reads normally', readLetTooEarly()[1], 500);
test('a var reads as undefined instead of throwing', readVarTooEarly(), ['undefined', 'number']);
test('even typeof throws inside the dead zone', typeofInTheDeadZone(), 'ReferenceError');
test('shadowing hides the outer value for the whole block', shadowedInTheDeadZone(), 'ReferenceError');
test('the outer binding itself is untouched', outerRate, 'outer');
test('a function declaration is hoisted whole, so it can be called above itself', callsBeforeDefined(), 8);
The dead zone is not "the variable does not exist yet". The binding exists from the top of the block — that is precisely why the fifth assertion gives a ReferenceError instead of quietly reading 'outer'. Once you write let outerRate anywhere in a block, the outer outerRate is unreachable throughout that block.
That is a feature. With var the same code would have read undefined and carried on, and the bug would surface somewhere else entirely. Function declarations remain fully hoisted, which is the one place calling above the definition is still fine.