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

Nullish Coalescing (??) & Logical Assignment

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

Closest to 'default linter catches the common case' (d3). ESLint and TypeScript can detect || used where ?? is more appropriate (code_pattern explicitly mentions this), catching the most common mistake automatically.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix is explicit: replace || with ??. This is a mechanical substitution that requires no refactoring, no parameter changes, and no cross-file coordination.

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

Closest to 'minimal commitment' (b1). This is pure syntax choice—a naming convention for 'how to write default values'—with no load-bearing or cross-cutting implications. Once a team adopts ?? for defaults, every future maintainer simply writes new code the same way. No component is shaped by the choice.

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

Closest to 'notable trap' (t5). The misconception ('?? and || are interchangeable') is the canonical gotcha. Developers with || muscle memory from other languages will assume both behave the same way; many do eventually learn the distinction. The common_mistakes confirm this is documented and expected to trip people up, but it's not a catastrophic misunderstanding—once explained, the logic is sound.

About DEBT scoring →

Also Known As

nullish coalescing ?? operator ??= assignment null coalescing JS

TL;DR

?? returns the right side only when the left is null or undefined — unlike ||, it does not trigger on 0 or empty string. Parallel to PHP's null coalescing operator.

Explanation

?? (ES2020) vs ||: ?? only checks null/undefined; || checks all falsy values (0, '', false, NaN). For API responses with 0 counts or empty strings, ?? prevents incorrect fallbacks. Logical assignment: ??= assigns only if null/undefined, ||= only if falsy, &&= only if truthy. PHP's ?? operator has the same semantics as JS ??. Optional chaining ?. accesses deeply nested properties safely without null checks.

Common Misconception

?? and || are interchangeable — || treats 0, '', false, and NaN as falsy and returns the right side; ?? only triggers for null and undefined, which is almost always what you want for default values.

Why It Matters

PHP APIs returning 0 for counts or empty strings for optional fields will incorrectly trigger || fallbacks, masking real data — ?? prevents these bugs.

Common Mistakes

  • Using || for default values when 0 or empty string are valid response values
  • Confusing ?. (optional chaining) with ?? (nullish coalescing)
  • ??= not well-supported before 2021

Code Examples

✗ Vulnerable
// || incorrectly overrides valid 0 count:
const count = apiResponse.count || 0; // If count is 0, stays 0... wait no: 0 || 0 = 0
const label = apiResponse.label || 'Default'; // If label is '' — uses 'Default' incorrectly

// PHP parallel that also has this bug:
// $label = $response['label'] ?: 'Default'; // '' triggers fallback
✓ Fixed
// ?? only falls back on null/undefined:
const count = apiResponse.count ?? 0;   // 0 count preserved
const label = apiResponse.label ?? 'Default'; // '' label preserved

// Optional chaining + nullish coalescing:
const city = user?.address?.city ?? 'Unknown';

// Logical assignment — lazy initialise:
cache[key] ??= expensiveCompute(key);

// PHP parallel:
// $city = $user['address']['city'] ?? 'Unknown';

Added 17 Mar 2026
Edited 22 Mar 2026
Views 60
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 1 ping M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 4 pings S 0 pings M 4 pings T 0 pings W 0 pings T 1 ping F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F
No pings yet today
Applebot 1
Amazonbot 7 ChatGPT 6 Scrapy 5 Google 4 Ahrefs 4 Bing 4 Perplexity 3 Unknown AI 3 SEMrush 3 PetalBot 3 Twitter/X 2 Majestic 1 Meta AI 1 Applebot 1
crawler 41 crawler_json 5 pre-tracking 1
🧱 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 →
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Low
⚡ Quick Fix
Replace || with ?? for default values when 0, empty string, or false are valid data — ?? only triggers for null and undefined like PHP's ??
📦 Applies To
javascript ES2020 web cli
🔗 Prerequisites
🔍 Detection Hints
|| used for default values where 0 or '' are valid API response values
Auto-detectable: ✓ Yes eslint typescript
🤖 AI Agent
Confidence: Medium False Positives: Medium ✓ Auto-fixable Fix: Low Context: Line


✓ schema.org compliant