const count = 0;
count || 10; // 10 — WRONG if 0 is a valid value!
count ?? 10; // 0 — correct, 0 is not nullishTip
Use this exact 0-vs-empty-string example — it's the classic reason ?? was added to the language.
GoCareerGo Tutorials
Variables to closures, promises, the DOM and OOP — a complete JS reference.
Returns the right-hand value ONLY when the left-hand value is null or undefined — unlike || which also triggers on 0, '' and false.
const count = 0;
count || 10; // 10 — WRONG if 0 is a valid value!
count ?? 10; // 0 — correct, 0 is not nullishUse this exact 0-vs-empty-string example — it's the classic reason ?? was added to the language.