freecoding.school100% FREE · NO SIGNUP
Script SpringsISSUE #19 of 29

operators · nullish · ??

SparkyVSNaN the Trickster
Sparky saysThe nullish coalescing operator ?? supplies a default only for null or undefined.

a ?? b returns a unless it's null or undefined, in which case b. Unlike ||, it treats 0, "", and false as valid values — exactly what you want for numeric or boolean settings. Pair it with optional chaining (?.) for safe defaults.

Power-ups you unlock

NaN the Trickster attacks — common mistakes

Boss battleShow ?? keeping a 0 value where || would replace it.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>const qty=0;
document.getElementById('o').textContent='qty ?? 5 = '+(qty ?? 5)+'\nqty || 5 = '+(qty || 5);</script></body></html>
▶ Open the interactive comic issue
‹ Operators · Logical · && · || · !Operators · Ternary · Short-Circuit ›