0Pricing
JavaScript Academy · Lesson

Combining ?. and ??

Pair safe access with sensible defaults.

Two Tools Together

Optional chaining ?. safely reads possibly-missing values, and nullish coalescing ?? supplies a default. Combined, they read deep data and fall back cleanly.

const user = {};
console.log(user.profile?.name ?? "Anonymous");

The Common Pattern

The idiom a?.b ?? fallback reads safely and defaults if the result is nullish. This replaces verbose guard chains.

const data = { settings: { theme: "dark" } };
const theme = data.settings?.theme ?? "light";
console.log(theme); // "dark"

All lessons in this course

  1. Optional Chaining with the ?. Operator
  2. Nullish Coalescing with ??
  3. Combining ?. and ??
  4. Optional Calls and Index Access
← Back to JavaScript Academy