JS Quiz: Medium: Temporal dead zone edge case

What happens when this runs?

{
  console.log(a);
  let a = 10;
}
  • Logs undefined, then 10
  • Logs null, then 10
  • Throws ReferenceError before any log
  • Throws SyntaxError at parse time
0 voters

Sora

It throws a ReferenceError before anything logs.

let a is block-scoped and hoisted, but it’s uninitialized until the declaration executes. So console.log(a) tries to read a while it’s still in the temporal dead zone for that {} block, and the runtime bails immediately.

This one dies immediately with a ReferenceError before anything logs. let a exists for the whole {} block, but it’s basically “reserved” until the let a = ... line runs, so the earlier console.log(a) is trying to read it while it’s still in the temporal dead zone.

It’s like putting a label on an empty box — everyone agrees the box is “there,” but you’re not allowed to open it yet.

Yep — this is the classic TDZ behavior for let/const: the binding exists for the whole block, but any access before the declaration line executes throws a ReferenceError, so console.log(a) never runs.

kirupa has a nice walkthrough of this exact “why doesn’t it just log undefined?” confusion here: https://www.kirupa.com/javascript/what_is_the_temporal_dead_zone.htm

look — let and const stay in the TDZ until that declaration line runs, so console.log(a) blows up before it can print anything.

var would give you undefined instead, which is how people end up chasing the wrong bug for half a day.