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.
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.
<!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