0Pricing
JavaScript Academy · Lesson

Combining ?. and ??

Pair safe access with sensible defaults.

Combining ?. and ?? is a free JavaScript Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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"

Falling Back When Missing

If any link is nullish, ?. yields undefined, which ?? then replaces with the default.

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

Deep Nesting

The pattern scales to deeply nested structures while keeping a single fallback at the end.

const res = { data: { user: { city: "Berlin" } } };
const city = res.data?.user?.city ?? "Unknown";
console.log(city); // "Berlin"

Keeping Valid Falsy Values

Because ?? only defaults on nullish, real 0 values from the chain are preserved.

const cart = { items: [], count: 0 };
const count = cart?.count ?? 5;
console.log(count); // 0

Versus the && / || Trap

Writing a && a.b || fallback would wrongly replace a legitimate 0 or "". The ?. + ?? combo avoids that.

const cfg = { volume: 0 };
console.log(cfg && cfg.volume || 50); // 50, wrong
console.log(cfg?.volume ?? 50);      // 0, correct

With Function Results

You can default the result of an optional call too, combining both operators around a method invocation.

const api = { fetchName: () => null };
const name = api.fetchName?.() ?? "default";
console.log(name); // "default"

Defaulting Array Access

The pattern works with bracket access for arrays or dynamic keys.

const state = { list: ["a"] };
const first = state.list?.[0] ?? "empty";
console.log(first); // "a"

Building Display Strings

A practical use is producing safe display text from uncertain data.

function label(order) {
  return "Total: " + (order?.total ?? 0);
}
console.log(label(undefined)); // "Total: 0"

Precedence Note

?. binds tighter than ??, so a?.b ?? c reads as (a?.b) ?? c, exactly what you want.

const u = { age: null };
console.log(u?.age ?? 18); // 18

A Clean Idiom

Together these operators replace long defensive code with one readable line, making intent obvious.

const profile = null;
console.log(profile?.bio?.length ?? 0); // 0

Quick Check

Predict the output.

Recap

The idiom obj?.deep?.prop ?? fallback reads possibly-missing nested values and supplies a default only when the result is null or undefined. It preserves valid falsy values like 0 and "", unlike the &&/|| approach. ?. binds tighter than ??, so the grouping is naturally correct.

Frequently asked questions

Is the “Combining ?. and ??” lesson free?

Yes — the full text of “Combining ?. and ??” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Combining ?. and ??”?

Pair safe access with sensible defaults. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Combining ?. and ??” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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