Optional chaining (?.)
Accesses a nested property or calls a method, short-circuiting to undefined if the reference is null or undefined.
Syntax
obj?.prop
obj?.[expr]
fn?.(args)
Returns
any — The accessed value, or `undefined` if a link in the chain is `null`
or `undefined`.
Examples
const user = { profile: { name: "Ada" } };
console.log(user?.profile?.name);
console.log(user?.address?.city);
Output
Ada
undefined
const obj = {};
console.log(obj.fn?.());
Output
undefined
const arr = null;
console.log(arr?.[0]);
Output
undefined
Notes
- Short-circuits the entire chain when the operand before `?.` is
`null` or `undefined`.
- Only `null` and `undefined` trigger short-circuiting; other falsy
values do not.
- Often paired with the nullish coalescing operator (`??`).
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 80 |
| firefox | 74 |
| safari | 13.1 |
| edge | 80 |
| node | 14.0 |