JavaScript Academy · Lesson

Combining ?. and ??

Pair safe access with sensible defaults.

Lesson 3 of 413 steps

Combining ?. and ?? is a free JavaScript Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.

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.

Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

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

Yes — any 3 lessons of the JavaScript Academy learning path, including “Combining ?. and ??”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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