Glossary / Temporal dead zone

Temporal dead zone

The temporal dead zone (TDZ) is the time between entering a lexical scope and initializing a let, const, or class binding. The binding already exists in the scope, but reading or writing it before initialization throws a ReferenceError. This prevents code from silently using an unintended value before the declaration has executed.

Example and boundaries

{
  // console.log(status); // ReferenceError: status is in its TDZ
  let status = "ready";
  console.log(status); // "ready"
}

The TDZ begins when the scope is entered, not simply at the declaration’s textual line. It applies to the binding, so typeof status does not bypass the error when status is a lexical binding in its TDZ. This differs from typeof on an entirely undeclared identifier, which normally returns "undefined".

Initialization order can expose TDZ errors through destructuring defaults, parameter defaults, and cyclic module dependencies. For example, a module can import a live binding from another module that has not yet initialized it when a cycle triggers early access. Move initialization dependencies into explicit functions or redesign cycles rather than adding defensive typeof checks.

var does not use a TDZ: it is initialized to undefined during declaration instantiation. That permissive behavior can hide order bugs, which is one reason lexical declarations are preferred.

Debugging and design

When a TDZ error occurs, find the binding and the order in which its scope is evaluated. Check for shadowed names, circular imports, default initializers that read later parameters, and callbacks invoked during initialization. Do not “fix” it by changing const to var unless the intended semantics truly require function scope.

Interview questions

  1. Is a TDZ variable undeclared? No. It is a known lexical binding that has not initialized.
  2. Does typeof avoid the TDZ? No; it throws for a lexical binding in the TDZ.
  3. When does TDZ begin? On entering the scope, before the binding’s initialization point.
  4. What can create TDZ problems between files? Cyclic module evaluation that reads an imported live binding too early.

References