← Back to JavaScript, page 9

javascript-0167 · Medium mid

Which of these temporal dead zone puzzles throw, which print a value, and what rule explains each?

let, const, and class declarations are hoisted but not initialized, so reading them before their line runs throws a ReferenceError for the whole block from its start. This also applies to typeof, to default parameters that refer to later parameters, and to a variable used in its own initializer. A function called later is fine, because the check happens when the variable is read, not when it is written in code.

Full answer

The temporal dead zone (TDZ) is the span from the start of a scope to the line where a let, const, or class declaration is evaluated. In that span the binding exists but is uninitialized, and any read or write, including typeof, throws a ReferenceError.

The puzzles and their rules:

  1. Reading before the line: console.log(a); let a = 1; throws. With var it prints undefined, because var is initialized to undefined at the start of the function.
  2. typeof is not a safe check inside the TDZ: typeof b; let b; throws, although typeof of a name that is never declared returns 'undefined'.
  3. Self reference: let x = x; throws, because the right side runs while x is still in the dead zone. var x = x gives undefined.
  4. Default parameters have their own scope and are evaluated left to right: function f(a = b, b = 1) throws if a uses the default, because b is not initialized yet. function f(a = 1, b = a) works.
  5. Function bodies called later: function show() { return value; } let value = 5; show(); works if show is called after the declaration ran. If show() is called before, it throws, because the check is at read time.
  6. Shadowing: an inner let with the same name as an outer variable creates a dead zone for the inner block, even for earlier lines: let v = 1; { console.log(v); let v = 2; } throws, and it does not print the outer 1.
  7. Classes: new Foo(); class Foo {} throws. A class is hoisted like let, unlike a function declaration, which is fully hoisted.
  8. switch cases share one block scope: switch (1) { case 0: let t = 1; break; case 1: console.log(t); } throws, because the case that declares t was never run. Use braces around the case body.
  9. const and let in loops are per iteration, so for (let i = 0; ...) has no TDZ problem, but for (let i = i; ...) does.

Why it exists: it catches use-before-define bugs and makes const meaningful. A cheap rule: declare variables at the top of their scope before using them.

javascript
const attempt = (label, fn) => {
  try {
    console.log(label.padEnd(34), '->', fn());
  } catch (error) {
    console.log(label.padEnd(34), '->', error.name);
  }
};

attempt('1. let read before declaration', () => { const read = () => value; const result = read(); let value = 1; return result; });
attempt('1b. var read before declaration', () => { const result = early; var early = 1; return result; });
attempt('2. typeof in the dead zone', () => { const t = typeof inside; let inside = 1; return t; });
attempt('2b. typeof of an undeclared name', () => typeof neverDeclared);
attempt('3. let x = x', () => { let x = x; return x; });
attempt('3b. var x = x', () => { var x = x; return x; });
attempt('4. default uses a later parameter', () => ((a = b, b = 1) => [a, b])());
attempt('4b. default uses an earlier one', () => ((a = 1, b = a + 1) => [a, b])());
attempt('4c. only the second argument given', () => ((a = b, b = 1) => [a, b])(undefined, 5));

attempt('5. function called after declaration', () => {
  function show() { return later; }
  let later = 5;
  return show();
});
attempt('5b. function called before declaration', () => {
  function show() { return later; }
  const result = show();
  let later = 5;
  return result;
});

attempt('6. inner let shadows the outer one', () => {
  let v = 1;
  {
    const seen = () => v;
    const out = seen();
    let v = 2;
    return [out, v];
  }
});
attempt('6b. without the inner declaration', () => {
  let v = 1;
  { return v; }
});

attempt('7. class used before definition', () => { const instance = new Foo(); class Foo {} return instance; });
attempt('7b. function declaration is hoisted', () => { const result = hoisted(); function hoisted() { return 'ok'; } return result; });

attempt('8. switch cases share one scope', () => {
  switch (1) {
    case 0: let t = 'zero'; break;
    case 1: return t;
  }
});
attempt('8b. braces fix it', () => {
  switch (0) {
    case 0: { let t = 'zero'; return t; }
  }
});

attempt('9. for (let i = i; ...)', () => { for (let i = i; i < 1; i++) return i; });
attempt('9b. normal let loop', () => { const out = []; for (let i = 0; i < 3; i++) out.push(() => i); return out.map((f) => f()); });
attempt('10. const assignment after init', () => { const c = 1; c = 2; return c; });

Run with Node.js v24.22.1 as js-167.js; two runs gave identical output. Output: 1. let read before declaration -> ReferenceError 1b. var read before declaration -> undefined 2. typeof in the dead zone -> ReferenceError 2b. typeof of an undeclared name -> undefined 3. let x = x -> ReferenceError 3b. var x = x -> undefined 4. default uses a later parameter -> ReferenceError 4b. default uses an earlier one -> [ 1, 2 ] 4c. only the second argument given -> ReferenceError 5. function called after declaration -> 5 5b. function called before declaration -> ReferenceError 6. inner let shadows the outer one -> ReferenceError 6b. without the inner declaration -> 1 7. class used before definition -> ReferenceError 7b. function declaration is hoisted -> ok 8. switch cases share one scope -> ReferenceError 8b. braces fix it -> zero 9. for (let i = i; ...) -> ReferenceError 9b. normal let loop -> [ 0, 1, 2 ] 10. const assignment after init -> TypeError

Key points

  • let, const, and class are hoisted but uninitialized until their declaration runs; reading earlier throws ReferenceError.
  • typeof does not protect against the TDZ; var gives undefined instead.
  • Default parameters are evaluated left to right and cannot use later parameters.
  • An inner let creates a dead zone for the whole inner block, hiding the outer variable.
  • Functions can use later declarations if they are called afterwards; switch cases need braces.

Common mistakes

  • Believing that let and const are not hoisted at all.
  • Using typeof to guard a variable that is declared later in the same scope.
  • Using a later parameter in an earlier default value.
  • Declaring variables inside switch cases without braces.

Follow-ups

  • What is the difference between the temporal dead zone and var hoisting?
  • Why does a class behave like let and not like a function declaration?
  • How does the TDZ show up with circular imports in ES modules?

Tips

  • Say that the binding exists from the start of the scope but is uninitialized.
  • Walk through the shadowing puzzle, since it surprises most people.
  • Give the practical rule: declare before use and use braces in switch cases.