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

undefined vs null — Subtle Differences

JavaScript ES5 Beginner
debt(d5/e3/b3/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list ESLint and TypeScript as tools. ESLint with eqeqeq rule catches loose == comparisons, and TypeScript with strict null checks catches type mismatches, but neither is a default out-of-the-box configuration — you need to opt into strict modes or specific rules. The JSON serialisation difference is silent at runtime unless tested explicitly.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix is concrete: replace explicit undefined assignments with null, swap == with ===, adopt ?? for nullish coalescing, and enable TypeScript strict null checks. This is a small pattern-replacement effort within a component, not a one-liner but not a multi-file architectural change.

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

Closest to 'localised tax' (b3). The applies_to covers web and cli broadly, but the confusion is localised to specific comparison and serialisation sites. It imposes a persistent awareness tax on developers writing comparisons or API contracts, but doesn't reshape the whole codebase architecture.

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

Closest to 'serious trap' (t7). The misconception field explicitly states that developers treat null and undefined as interchangeable — a belief reinforced by the fact that null == undefined is true under loose equality. The JSON.stringify behaviour difference (drops undefined, preserves null) contradicts what most developers expect, especially given how similar the two values appear semantically.

About DEBT scoring →

TL;DR

undefined means 'not yet assigned' (declared but empty); null means 'intentionally absent' — they're different values but both falsy, and typeof null === 'object' is a famous JS bug.

Explanation

undefined: default value of unassigned variables, missing object properties, missing function parameters, void function return. null: explicit absence, intentional empty value. Key differences: typeof undefined === 'undefined', typeof null === 'object' (JS bug from 1995, can't fix). null == undefined (true), null === undefined (false). JSON.stringify converts undefined to nothing, null to 'null'. Optional chaining: obj?.prop returns undefined for missing, not null. Best practice: use null for intentional absence, never assign undefined explicitly.

Common Misconception

null and undefined are interchangeable — they're different values. null == undefined (loose) but null !== undefined (strict). JSON treats them differently.

Why It Matters

Confusing null and undefined causes subtle bugs in comparisons and JSON serialisation — especially in API contracts where null means 'explicitly empty' vs missing field.

Common Mistakes

  • Assigning undefined explicitly — use null for intentional absence.
  • Using == instead of === which treats null and undefined as equal.
  • Forgetting that JSON.stringify drops undefined values but preserves null.

Code Examples

✗ Vulnerable
// Both are falsy but behave differently:
const a = null;
const b = undefined;
console.log(a == b);   // true
console.log(a === b);  // false
console.log(JSON.stringify({a, b})); // {"a":null}  (b dropped!)
✓ Fixed
// Explicit null for intentional absence:
const user = fetchUser() ?? null; // null if not found

// Strict null checks:
if (value === null) { /* intentionally empty */ }
if (value === undefined) { /* not yet set */ }
if (value == null) { /* either null or undefined */ }

// TypeScript helps:
function getUser(id: string): User | null { }

Added 22 Mar 2026
Edited 5 Apr 2026
Views 85
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S 1 ping S 1 ping M 0 pings T 0 pings W 1 ping T 2 pings F 1 ping S 1 ping S 0 pings M 0 pings T 1 ping W 0 pings T 1 ping F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 2 pings S 1 ping S 0 pings M
No pings yet today
Brave Search 1
Google 9 Amazonbot 8 Scrapy 7 Perplexity 5 Brave Search 5 Unknown AI 4 Ahrefs 4 PetalBot 4 SEMrush 3 ChatGPT 2 Majestic 2 Meta AI 2 Claude 2 Sogou 2 Twitter/X 1 Bing 1 Applebot 1
crawler 57 crawler_json 2 pre-tracking 3
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
JavaScript javascript The programming language of the browser — it reads and modifies the page (the DOM), reacts to user events, and fetches data without reloading.

JavaScript is the only language browsers execute, so every interactive behaviour on the web goes through it. Its two defining traits — single-threaded event loop and loose typing (== coercion) — explain the majority of both its bugs and its design patterns.

💡 Default to const, use === always, and reach for let only when a value genuinely reassigns.

Ask Codex about JavaScript →
undefined javascript undefined is a special value in JavaScript that means a variable exists but hasn't been given a value yet, or a function didn't return anything.

Understanding undefined helps you debug missing data, handle optional values gracefully, and write defensive code that doesn't crash when something is unexpectedly absent.

💡 Always check if a value is undefined before calling methods on it—use optional chaining (`value?.method()`) as a shortcut.

Ask Codex about undefined →
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Use null for intentional absence, never assign undefined explicitly. Use === for comparisons. Use ?? (nullish coalescing) to handle both. Use TypeScript strict null checks.
📦 Applies To
javascript ES5 web cli
🔗 Prerequisites
🔍 Detection Hints
=== undefined|== null
Auto-detectable: ✓ Yes eslint typescript
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: High ✗ Manual fix Fix: Low Context: Line


✓ schema.org compliant