Hiprup

What is the Temporal Dead Zone (TDZ)?

The Temporal Dead Zone is the gap between entering a scope and the line where a let or const variable is declared. Accessing it during that window throws a ReferenceError.

  • Why it exists — let/const are hoisted but not initialised, so they can't be used early.

  • Versus var — var would give undefined instead of an error.

Benefit: the TDZ catches use-before-declaration bugs that var silently allowed.

// var — no TDZ (hoisted as undefined)
console.log(a); // undefined
var a = 5;

// let — TDZ (hoisted but not initialized)
// console.log(b); // ReferenceError: Cannot access 'b' before initialization
let b = 10;

// TDZ in action
{
  // TDZ for x starts here
  // console.log(x); // ReferenceError!
  let x = 42;       // TDZ ends here
  console.log(x);    // 42 — OK
}

// TDZ applies to typeof too!
// console.log(typeof undeclaredVar); // 'undefined' (not declared at all)
// console.log(typeof tdzVar);        // ReferenceError! (declared but in TDZ)
// let tdzVar = 1;

// TDZ in function default parameters
// function foo(a = b, b = 1) {} // ReferenceError! b is in TDZ when used as default for a

var a is hoisted and initialized to undefined — accessible before the declaration. let b is hoisted but stays in the TDZ — accessing before declaration throws ReferenceError. typeof on a TDZ variable also throws (unlike undeclared variables which return 'undefined'). Function defaults are evaluated left-to-right: a = b fails because b is still in its TDZ.

TDZ applies to let/const/class — they are hoisted but not initialized. The typeof gotcha (ReferenceError for TDZ vs 'undefined' for undeclared) is commonly asked.

The function parameter TDZ (a = b, b = 1) is an advanced detail.

What is the Temporal Dead Zone (TDZ)? | Hiprup