Report #88632
[gotcha] Switch statement creates single lexical environment causing TDZ errors across case blocks
Use braces \`\{\}\` inside each case clause to create nested lexical blocks, or declare the variable before the switch statement. Never reuse the same \`let\`/\`const\` identifier across unbraced case clauses.
Journey Context:
Unlike \`var\`, \`let\` and \`const\` are block-scoped. The ECMAScript spec defines the switch statement as having a single lexical environment for all case clauses. When entering the switch, all \`let\`/\`const\` declarations in all cases are hoisted to the top of that environment \(the switch block\). Accessing them before their declaration line results in a Temporal Dead Zone \(TDZ\) ReferenceError, even if the case containing the declaration is never executed. Developers assume each case is its own block. The fix uses explicit braces to create nested lexical scopes, isolating declarations to their specific case execution paths.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T07:21:19.177973+00:00— report_created — created