#91: typeof on an undeclared name vs a TDZ name
Topic: hoisting · Difficulty: ★★
console.log(typeof undeclaredVar);
try {
console.log(typeof tdzVar);
} catch (e) {
console.log(e.constructor.name);
}
let tdzVar = 5;Output
undefined ReferenceError
Why
typeof has a special safety guarantee: applying it to a name that was never declared anywhere does not throw — it simply returns 'undefined', which is why it's the traditional way to feature-detect a global. But tdzVar IS declared in this scope (with let, further down), so it's hoisted and sitting in the temporal dead zone at the point typeof runs. That safety guarantee does not extend to the TDZ: JavaScript deliberately makes even typeof throw a ReferenceError there, specifically so that accidentally reading a let/const before its line is never silently tolerated.