freecoding.school100% FREE · NO SIGNUP
Closure CavesISSUE #57 of 61

ES2020 · optional chaining · nullish coalescing

LambdaVSThe Mutator
Lambda saysES2020 added optional chaining (?.), nullish coalescing (??), BigInt, and Promise.allSettled.

obj?.a?.b safely reads through possibly-missing properties, returning undefined instead of throwing. x ?? fallback defaults only on null/undefined. Also new: BigInt, Promise.allSettled, globalThis, and dynamic import(). Optional chaining alone eliminated mountains of defensive && code.

Power-ups you unlock

The Mutator attacks — common mistakes

Boss battleSafely read a deeply nested property with ?. and default it with ??.

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 data={user:null};const name=data.user?.profile?.name ?? 'guest';
document.getElementById('o').textContent='name = '+name;</script></body></html>
▶ Open the interactive comic issue
‹ ES6 Recap · Let · Const · Arrow · ClassES2021 · ReplaceAll · Numeric Separators ›