What do optional chaining (?.) and nullish coalescing (??) do?

BeginnerJavaScript interview questionBy , updated 17 November 2026

Short answer

Optional chaining (?.) reads a property, index or method only if the value before it is not null or undefined; otherwise the whole expression returns undefined instead of throwing. Nullish coalescing (??) returns its right side only when the left side is null or undefined. Together, user?.address?.city ?? "Unknown" reads a nested value safely and supplies a default.

Optional chaining: ?.

Without it, reading through a missing object throws:

const user = { name: "Asha" };

user.address.city;   // TypeError: Cannot read properties of undefined (reading 'city')
user.address?.city;  // undefined

?. checks the value on its left. If that value is null or undefined, it stops and returns undefined. Any other value, including 0, "" and false, is accessed as normal.

It has three forms:

user?.name;            // property
user?.["first-name"];  // computed property or array index: list?.[0]
user.greet?.();        // call, only if greet is not null or undefined

The call form checks the function itself, so onSave?.() is a common way to call an optional callback. It only guards against null and undefined. If greet is a number or a string, calling it still throws TypeError: user.greet is not a function.

Short-circuiting

When ?. stops, it skips the rest of the chain, not just the next step:

const a = null;
a?.b.c.d;              // undefined, no error from .c or .d

let calls = 0;
a?.method(calls++);    // undefined
console.log(calls);    // 0: the argument was never evaluated

Short-circuiting only covers the part after the ?.. In user?.address.city, the ?. protects against a missing user. If user exists but address does not, .city still throws. Put ?. at each step that can genuinely be missing.

Parentheses end the chain. (a?.b).c evaluates a?.b to undefined first, and then .c throws.

What ?. cannot do

  • Assign. user?.name = "Ravi" is a SyntaxError. Check first, then assign.
  • Guard an undeclared variable. missingVar?.x still throws a ReferenceError. ?. only handles values that are null or undefined, not names that do not exist.
  • Tell "missing" from "set to undefined". To check whether a key exists, see how to check if an object has a property.

Nullish coalescing: ??

a ?? b is a, unless a is null or undefined, in which case it is b. The point is that it does not treat other falsy values as missing.

const settings = { volume: 0, title: "", muted: false };

settings.volume || 50;   // 50: wrong, 0 was a real value
settings.volume ?? 50;   // 0

settings.title || "Untitled";  // "Untitled"
settings.title ?? "Untitled";  // ""

settings.muted ?? true;  // false
settings.theme ?? "dark"; // "dark": the property is missing

?? vs ||

Left side left || "default" left ?? "default"
undefined "default" "default"
null "default" "default"
0 "default" 0
"" "default" ""
false "default" false
NaN "default" NaN
"hi" "hi" "hi"

Use ?? when 0, "" or false are valid values: counts, prices, form fields, feature flags. Use || when you really want any falsy value replaced, for example turning an empty string into a placeholder.

Both operators short-circuit: the right side is only evaluated when it is needed, so value ?? expensiveDefault() calls the function only when value is nullish.

Mixing ?? with || or &&

You cannot combine ?? with || or && without parentheses. The language makes it a syntax error so the order is never ambiguous:

a || b ?? c;      // SyntaxError
(a || b) ?? c;    // fine
a || (b ?? c);    // fine

??= and its relatives

x ??= y assigns y only if x is currently null or undefined. It is the short form of x ?? (x = y), not x = x ?? y: when x already has a value, no assignment happens at all, and y is not evaluated.

const options = { retries: 0 };
options.retries ??= 3;   // stays 0
options.timeout ??= 5000; // set to 5000

const cache = {};
(cache.users ??= []).push("asha"); // create the array on first use

||= assigns when the value is falsy and &&= assigns when it is truthy. Pick ??= for defaults unless you mean to replace every falsy value.

How they relate to default parameters

Default parameters and destructuring defaults apply only to undefined, not null:

function greet(name = "friend") {
  return `Hi ${name}`;
}
greet(undefined); // "Hi friend"
greet(null);      // "Hi null"

function greetSafe(name) {
  return `Hi ${name ?? "friend"}`;
}
greetSafe(null);  // "Hi friend"

APIs often return null for missing fields, so ?? is the safer default for data from a server. The null vs undefined answer explains where each one comes from, and destructuring covers default values in patterns.

Practise it

Predict ES6 results tests ?., ?? versus || and destructuring defaults. Predict logical assignment and short-circuit results adds ??=, optional calls and side effects, and Implement get(object, "a.b.c") with a default value asks you to build safe path access yourself.

Common follow-up questions

Is a?.b the same as a && a.b?

Nearly, but not quite. a && a.b returns a itself when a is any falsy value: with const s = "", s && s.length gives "", while s?.length gives 0. a?.b only stops for null and undefined, and when it stops it always returns undefined, even if a was null.

Can you use ?. with delete?

Yes. delete user?.address does nothing and returns true when user is nullish.

Should every property access use ?.?

No. Use it where a value can legitimately be missing, such as optional API fields or optional props. Sprinkling it everywhere hides real bugs: a value that should always exist silently becomes undefined instead of failing where the problem is.

Do these work in all browsers?

Both are part of ES2020 and ??= of ES2021. They work in all current browsers and Node. Build tools only need to transpile them for very old targets.

Mistakes to avoid

  • Using || for defaults when 0, "" or false are valid values

  • Writing a?.b.c and assuming it also protects against a missing b

  • Expecting fn?.() to guard against a value that is not a function

  • Mixing ?? with || or && without parentheses

  • Relying on default parameters to catch null

  • Adding ?. to paths that should never be missing and hiding the real error

Practise it in code

Interviewers rarely stop at the definition. These questions open in an editor with tests.

  1. Predict ES6 results: destructuring defaults, spread, optional chaining and ?? versus ||Easy20 min
  2. Predict logical assignment and short-circuit results: ||=, &&=, ??=, optional calls and side effectsEasy20 min
  3. Implement get(object, "a.b.c") with a default valueEasy15 min