whatstheoutput_

#69: Reading a let variable before its line

Topic: hoisting · Difficulty: ★

try {
  console.log(y);
} catch (e) {
  console.log(e.constructor.name);
}
let y = 5;

Output

ReferenceError

Why

let and const declarations are hoisted too — the engine knows y exists from the very top of the block — but unlike var they are not initialized to undefined. From the start of the block until the declaration line actually runs, y sits in the 'temporal dead zone' (TDZ): any read or write throws a ReferenceError, even though the variable technically exists. This is a deliberate safety net that catches the exact bug var hoisting hides silently (reading a variable too early), by failing loudly instead of quietly returning undefined.

More hoisting puzzles

Play today's puzzle →