Modern JS (ES6+)
Optional chaining & nullish coalescing
Safely reading deep properties and defaulting only for null/undefined.
Optional chaining (?.) short-circuits to undefined instead of throwing when a value in a chain is null or undefined — no more long a && a.b && a.b.c guards.
`?.` is asking "if this door exists, open it" instead of crashing into a wall; `??` is a spare key used only when there is genuinely no key.
Key concepts
1
Nullish coalescing (??) returns the right-hand side only when the left is null or undefined — unlike ||, it does not treat 0, "" or false as missing.
Nullish coalescing??nullundefined||
2
Together they make reading optional, deeply nested data concise and bug-resistant.
javascript
const user = { profile: { name: 'Ada' } };
user.profile?.name; // 'Ada'
user.settings?.theme; // undefined (no throw)
user.settings?.theme ?? 'light'; // 'light'
const count = 0;
count || 5; // 5 (wrong — 0 is falsy)
count ?? 5; // 0 (correct)