Optional Chaining & Nullish Coalescing

Safely reading deeply nested properties, and providing defaults, without a pile of manual checks.

What is it?

Reading a nested property like user.address.city throws an error if user or address is missing. The old fix was a chain of checks: if (user && user.address && user.address.city). Optional chaining (?.) does this for you: it stops and returns undefined as soon as it hits something missing, instead of throwing.

Its partner, nullish coalescing (??), gives a default only when a value is null or undefined. Unlike ||, it won't replace valid values like 0 or "".

Explain like I'm 10

Optional chaining is like checking each door is unlocked before walking through, instead of crashing into a locked one. Nullish coalescing is a backup plan, 'if there's truly nothing here, use this', that won't override an answer you gave on purpose, like zero.

Examples

Optional chaining and nullish coalescing together

const user = { name: "Amara", address: null };

console.log(user.address.city);   // ❌ throws: Cannot read properties of null
console.log(user.address?.city);  // undefined — no crash

const city = user.address?.city ?? "Unknown city";
console.log(city); // "Unknown city"

const count = 0;
console.log(count || 10); // 10 — wrong! 0 is falsy, so || replaces it
console.log(count ?? 10); // 0 — right! ?? only replaces null/undefined

Optional chaining with function calls and arrays

const api = {
  getUser: null, // maybe not loaded yet
};

api.getUser?.(1);      // undefined — skipped, doesn't throw

const users = null;
console.log(users?.[0]); // undefined — safe even though users isn't an object at all

?.() guards a function call that might not exist, and ?.[...] guards bracket access, just as ?. guards a plain property.

How it works

?. checks whether the value just to its left is null or undefined. If so, the whole chain short-circuits to undefined without running the rest.

?? checks only its left side. If it's null or undefined, you get the right side. Otherwise you keep the left value, even if it's 0, "" or false.

Why does it exist?

Nested data that might be missing (like optional API fields) used to need long chains of && checks to avoid crashing. These two operators handle that common need directly, so code is safer and shorter.

When to use it

Use ?. when reading a property that might be missing at some level: optional API fields, optional config, DOM elements that may not exist. Use ?? for a default for missing values when valid values can be falsy, like 0 or "".

When not to use it

Don't add ?. everywhere on data you know is always there. It can hide a bug that should have thrown and warned you. Use || instead of ?? when you really want to replace any falsy value, not just null/undefined.

Common mistakes

  • Using ?. so freely that a truly broken or missing value is silently turned into undefined instead of raising a helpful error.

  • Using || for defaults when 0, "", or false are valid values you want to keep. ?? is usually safer.

  • Forgetting that ?. only guards against null/undefined, not against other unexpected types.

Practice exercises

  1. Easy:

    Use optional chaining to safely read user.profile.bio when profile might be missing.

  2. Medium:

    Use ?? to provide a default page size of 10 when a pageSize variable might be 0, null, or undefined — and explain why || would behave differently.

  3. Hard:

    Rewrite a function that used several && checks to safely access nested data, using optional chaining and nullish coalescing instead.

Interview questions

What does `?.` do?

It safely accesses a property, only continuing if the value to its left isn't null or undefined, otherwise short-circuiting to undefined instead of throwing.

How is `??` different from `||`?

?? only falls back when the left side is null or undefined; || falls back for any falsy value, including 0, "", and false.

Can you call a function with optional chaining?

Yes — obj.method?.() calls method only if it exists, otherwise evaluates to undefined.