Temporal Dead Zone Throws ReferenceError

Access a let or const binding before its declaration runs and the temporal dead zone throws a ReferenceError; var instead reads undefined from hoisting.

JavascriptData Integrity

Code

javascriptsnippet
31 lines
// let/const bindings are hoisted but stay uninitialized until the
// declaration line runs — that window is the temporal dead zone, and
// any access in it throws a ReferenceError instead of reading undefined
function probe() {
  try {
    return phase; // declared below: still inside the temporal dead zone
  } catch (error) {
    return `${error.constructor.name}: ${error.message}`;
  }
  let phase = 'run';
}

console.log(probe());

let phase = 'done';
console.log('after declaration:', phase);

// Reassignment of const fails with TypeError — a different error class
const limit = 3;
try {
  limit = 4;
} catch (error) {
  console.log('reassign const ->', error.constructor.name);
}

// var has no dead zone: hoisting initializes the binding to undefined
function probeVar() {
  return `var reads ${legacy}`;
  var legacy = 'hoisted';
}
console.log(probeVar());

Output

Captured from a sandboxed run of this snippet in the build pipeline.

ReferenceError: Cannot access 'phase' before initialization
after declaration: done
reassign const -> TypeError
var reads undefined

Inside the snippet

Everything happens inside the functions probe and probeVar.

Caveats

  • It is dependency-free in the strict sense — the import list, if any, is empty of packages.

Using it

Signature: function probe()

Watch for labeled lines in the run — after declaration:, reassign const -> — when the file runs.

Key statements worth quoting

return phase; // declared below: still inside the temporal dead zone
} catch (error) {
return `${error.constructor.name}: ${error.message}`;
return `var reads ${legacy}`;

Interactive Tools for This Topic

FAQ

Which packages does this snippet need?
None. Every symbol the code uses resolves inside the standard library. The file defines `probe`, `probeVar` at load time.