🎓 A1Learner
🔍
0 of 50 lessons completed

JavaScript The Temporal Dead Zone

The temporal dead zone is the gap between the start of a scope and the line that declares a let or const. Reading the name inside that gap throws a ReferenceError.

Reading let early throws

The read of total falls inside the dead zone. The catch logs the error name and message, then the declaration line runs.

try {
  console.log(total);
} catch (err) {
  console.log(err.name + ": " + err.message);
}
let total = 5;
console.log(total);

const has a dead zone too

A const name has the same gap. The early read throws, and the value reads fine after its line.

try {
  console.log(limit);
} catch (err) {
  console.log(err.name + ": " + err.message);
}
const limit = 8;
console.log(limit);

typeof cannot see through the zone

Even typeof throws inside the zone. After the declaration line, typeof reads the value type as normal.

try {
  console.log(typeof hidden);
} catch (err) {
  console.log(err.name);
}
let hidden = 1;
console.log(typeof hidden);

Example

try {
  console.log(price);
} catch (err) {
  console.log(err.name + ": " + err.message);
}
const price = 12;
console.log(price);

Key points

  • Temporal dead zone: the gap where a let or const name exists but cannot be used yet
  • The zone starts at the top of the block.
  • The zone ends at the declaration line.
  • Reading the name in the zone throws a ReferenceError.
  • typeof cannot see through the zone either.
  • var has no dead zone, so it reads as undefined instead.

In this example, the first read of price falls inside the dead zone, so it throws a ReferenceError. The catch logs the error name and message. After the const line, price reads as 12.

Try it Yourself

Edit the code, then press Run.

Runs in your browser — the preview under the editor is the real result. Nothing is sent to a server. The code runner panel (right) does the same with more room.

Exercise

What happens when code reads a let variable before its declaration line?