Sub topic 8
How Optional Chains Short-Circuit
This chapter assumes you already know how ?. works for property access, bracket access and calls, as covered in earlier chapters. It also assumes you are comfortable with JavaScript expressions that have side effects, such as count++ or a function call that logs output.
The basic rule is that ?. returns undefined when the value on its left is null or undefined. That rule leaves one question open: what happens to the code to the right of the ?.? The answer is short-circuiting. Once ?. sees a nullish value, JavaScript skips the rest of the chain.
The Whole Chain Is Skipped
An optional chain is the full sequence of property accesses, bracket accesses and calls that follows a ?.. When the check fails, JavaScript does not stop at the next step and return undefined there. It skips every later step in the chain, including any expressions inside brackets or argument lists.
let count = 0;
const obj = null;
obj?.prop[count++];
console.log(count); // 0 (the rest of the chain, including count++, never runs)
function expensive() {
console.log("expensive() ran");
return 42;
}
obj?.method(expensive()); // nothing is logged
console.log("done"); // "done"count++ never runs, and neither does expensive(). When obj is nullish, this behaves like a guarded if:
let count = 0;
const obj = null;
const result = obj == null ? undefined : obj.prop[count++];
console.log(result, count); // undefined 0The following diagram traces how a?.b.c(d()) is evaluated.
One `?.` Does Not Protect Later Links
Short-circuiting depends only on the value directly to the left of the ?.. In a?.b.c, the ?. guards a. It does not guard a.b. If a exists but a.b is undefined, reading .c still throws.
const missing = null;
const partial = { b: undefined };
console.log(missing?.b.c); // undefined (whole chain skipped)
try {
console.log(partial?.b.c);
} catch (err) {
console.log(err instanceof TypeError); // true: a.b is undefined
}
console.log(partial?.b?.c); // undefined (second ?. guards b)This is deliberate. Writing a?.b.c says "a may be missing, but if it exists, it must have b." Put a ?. only at the links that can really be absent.
Parentheses End the Chain
Wrapping part of a chain in parentheses turns it into a complete expression. Short-circuiting stops at the closing parenthesis, and whatever follows works on the undefined that the parenthesized part produced.
const obj = null;
console.log(obj?.prop.length); // undefined (.length is skipped)
try {
console.log((obj?.prop).length);
} catch (err) {
console.log(err.message); // Cannot read properties of undefined (reading 'length')
}| Expression | `obj` is `null` | Why |
|---|---|---|
| `obj?.prop.length` | `undefined` | `.length` is part of the skipped chain |
| `(obj?.prop).length` | `TypeError` | Parentheses end the chain, and `.length` reads from `undefined` |
| `obj?.prop?.length` | `undefined` | Skipped at the first `?.` |
Common Mistakes
- Expecting a side effect, such as a counter increment, logging call or network request, inside a skipped chain to run anyway. It does not run.
- Assuming one
?.near the start protects every later property. - Adding parentheses for readability and accidentally ending the chain.
Exercise
Given let n = 0; const cfg = null; const r = cfg?.items[n++].name;, what are the values of r and n? What changes if cfg is { items: [] }?
Show answer
With cfg = null, r is undefined and n is 0 because the whole chain, including n++, is skipped. With cfg = { items: [] }, the chain runs: n++ runs (so n becomes 1), cfg.items[0] is undefined, and reading .name on it throws a TypeError.
Exercise
Why does (user?.address).city throw when user is undefined, while user?.address.city does not?
Show answer
The parentheses end the optional chain. (user?.address) evaluates to undefined, and .city is then a normal property read on undefined, which throws. Without the parentheses, .city belongs to the chain and is skipped.