ReferenceError — Undefined Variables
debt(d2/e1/b1/t5)
Closest to 'caught instantly (compiler/syntax error)' (d1), but slightly worse: ESLint and TypeScript (both listed in detection_hints.tools) catch ReferenceErrors reliably at static analysis time, but only if tooling is configured and run; without them, runtime discovery is inevitable. Score d2 reflects that detection is nearly automatic in modern setups but requires explicit tool adoption.
Closest to 'one-line patch' (e1): quick_fix specifies 'Use typeof x !== "undefined" to check variable existence' or 'Declare all variables before use' — both are single-line or single-statement fixes with no cross-file refactoring required.
Closest to 'minimal commitment' (b1): ReferenceError handling is localized to the specific code site where the variable is accessed. Declaring variables and using typeof checks impose no architectural weight, no decay, and no load-bearing responsibility across the system.
Closest to 'notable trap' (t5): The misconception field explicitly states 'undefined and ReferenceError are the same' — a documented gotcha that most developers eventually learn. The Temporal Dead Zone (TDZ) with let/const is an additional subtle edge case. However, it does not contradict similar concepts elsewhere (the distinction is fairly intrinsic to JavaScript), so it falls short of t7.
TL;DR
Explanation
ReferenceError: x is not defined means x was never declared with var/let/const. This is different from undefined, which means a variable was declared but has no value. Common causes: typos in variable names, accessing variables before declaration (TDZ for let/const), accessing out-of-scope variables, using browser globals (window.something) in Node. typeof never throws ReferenceError — use typeof x !== 'undefined' to safely check if a variable exists without risk of throwing.
Common Misconception
Why It Matters
Common Mistakes
- Checking if (x) when x might not be declared — use if (typeof x !== 'undefined').
- Accessing let/const variables before their declaration in the same scope (TDZ).
- Relying on var hoisting — var is hoisted but initialised as undefined, not the assigned value.
Code Examples
console.log(userName); // ReferenceError: userName is not defined
// TDZ:
console.log(value); // ReferenceError: Cannot access 'value' before initialization
const value = 42;
// Safe existence check:
if (typeof userName !== 'undefined') {
console.log(userName);
}
// Always declare before use:
const value = 42;
console.log(value); // 42