← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

ReferenceError — Undefined Variables

JavaScript ES2015 Beginner
debt(d2/e1/b1/t5)
d2 Detectability Operational debt — how invisible misuse is to your safety net

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.

e1 Effort Remediation debt — work required to fix once spotted

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.

b1 Burden Structural debt — long-term weight of choosing wrong

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.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

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.

About DEBT scoring →

TL;DR

ReferenceError is thrown when accessing a variable that hasn't been declared — unlike undefined which is a declared variable with no value.

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

undefined and ReferenceError are the same — undefined is a value (declared variable), ReferenceError means the variable was never declared.

Why It Matters

ReferenceErrors in production crash user flows. They often indicate typos or scope issues that static analysis (TypeScript) would catch at compile time.

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

✗ Vulnerable
console.log(userName); // ReferenceError: userName is not defined

// TDZ:
console.log(value); // ReferenceError: Cannot access 'value' before initialization
const value = 42;
✓ Fixed
// Safe existence check:
if (typeof userName !== 'undefined') {
    console.log(userName);
}

// Always declare before use:
const value = 42;
console.log(value); // 42

Added 22 Mar 2026
Edited 5 Apr 2026
Views 48
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 2 pings T 3 pings F 2 pings S 1 ping S 1 ping M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W
SEMrush 1
No pings yesterday
Scrapy 10 Amazonbot 6 Unknown AI 4 Ahrefs 3 Majestic 2 Meta AI 2 Perplexity 2 Google 2 Claude 2 ChatGPT 1 Bing 1 SEMrush 1
crawler 31 crawler_json 2 pre-tracking 3
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Low
⚡ Quick Fix
Use typeof x !== 'undefined' to check variable existence. Declare all variables before use. Use TypeScript to catch undeclared variables at compile time.
📦 Applies To
javascript ES2015 web cli
🔗 Prerequisites
🔍 Detection Hints
ReferenceError
Auto-detectable: ✓ Yes eslint typescript
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Medium ✗ Manual fix Fix: Low Context: Function Tests: Update


✓ schema.org compliant