Nullish Coalescing (??) & Optional Chaining (?.) in JavaScript
Modern JavaScript's two safety operators: ?? defaults only on null/undefined (count ?? 10 keeps legitimate 0, unlike ||), and ?. short-circuits property access (user?.address?.city yields undefined instead of throwing on missing links). Together they replaced mountains of && guards and ternary defaults.
Rules that matter: ?? cannot mix unparenthesized with && /|| (syntax error forcing explicit grouping), ?. short-circuits the entire rest of the chain (a?.b.c() skips the call too), and ?. works for calls (fn?.()) and brackets (m?.[k]) as well as dots.
?? defaults narrowly
count ?? 10 keeps 0 and "" β only null/undefined fall through, unlike ||'s broad falsy net.
?. skips missing links
user?.address?.city evaluates to undefined when any link is nullish β no TypeError, no manual guards.
Short-circuits everything after
a?.b.c() skips property AND call when a is nullish β the whole tail vanishes safely.
No unparenthesized mixing
a ?? b || c is a syntax error β group explicitly ((a ?? b) || c) so precedence is always visible.
let user = { name: "Ada" };console.log(user?.name, user?.address?.city ?? "unknown");console.log(0 ?? 99, 0 || 99);Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself β building beats watching.