Optional Chaining, Nullish Coalescing, and Logical Assignment
ES2020 introduced three operators that quietly eliminated a huge amount of defensive boilerplate code.
const user = { profile: { name: "Rima" } };
console.log(user?.profile?.age); // undefined — no error, even though 'age' is missing
console.log(user?.address?.city); // undefined — no error, even though 'address' is missing
const port = process.env.PORT ?? 3000; // falls back only on null/undefined, not 0 or ""
let count;
count ??= 10; // assigns only if count is currently null/undefined
Optional chaining (?.) stops evaluating and returns undefined the moment it hits a null or undefined value, instead of throwing a TypeError — invaluable when working with deeply nested, not-always-complete API responses.
Nullish coalescing (??) is a safer alternative to || for supplying defaults, because it only falls back when the left side is null or undefined — not on other falsy values like 0, "", or false, which || would incorrectly treat as "missing." The logical assignment operators (??=, &&=, ||=, ES2021) combine an assignment with a logical check in one step, replacing patterns like count = count ?? 10.