Report #6908
[gotcha] typeof operand in Temporal Dead Zone throws ReferenceError instead of returning undefined
Never use \`typeof x === 'undefined'\` to check for block-scoped variables \(let/const\) that might not be initialized yet. Use a try-catch block around the typeof check, or restructure code so the variable is declared in an outer scope or initialized before the check.
Journey Context:
Developers traditionally use \`typeof foo === 'undefined'\` as a safe existence check that works for undeclared variables \(avoiding ReferenceError\). However, with ES6 let and const, variables exist in the Temporal Dead Zone \(TDZ\) from the start of the block until the declaration is executed. Accessing them in any way \(including typeof\) throws a ReferenceError because the binding exists but is uninitialized. This breaks the classic typeof guard pattern for block-scoped variables. The alternatives are to check if the variable is declared in an outer scope or use try-catch, but the real fix is to avoid checking for existence of block-scoped variables and instead structure code so initialization order is guaranteed.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T01:19:05.390034+00:00— report_created — created