What Is Optional Chaining (?.) in JavaScript?
Optional chaining (?.) lets you safely access a deeply nested property without throwing an error if an intermediate value is null or undefined, short-circuit...
Published November 21, 2026
Optional chaining (?.) safely reads a nested property or calls a method: if anything before ?. is null or undefined, the whole expression returns undefined instead of throwing an error.
The concept
user?.address?.city stops at the first missing link. It also works for method calls (obj.method?.()) and indexing (arr?.[0]).
Combine it with ?? to supply a fallback when the result is undefined or null.
Without vs with
user.address.city // TypeError if address is undefined
user?.address?.city // undefined
user?.address?.city ?? 'Unknown'
'Unknown'
Practical examples
Property access
const city = response?.data?.user?.address?.cityOptional method call
callback?.()
obj.onload?.(event)Array element
const first = list?.[0]?.titleWith default
const name = user?.profile?.name ?? 'Guest'Common mistakes
- Chaining everywhere and hiding real bugs.
- Expecting it to guard against 0 or '' (only null/undefined).
- Using it on the left side of an assignment (not allowed).
Key takeaways
- ?. short-circuits on null/undefined.
- Works for properties, calls and indexes.
- Pair with ??.
FAQ
?. vs &&?
&& also stops on 0, '' and false; ?. only on null/undefined.
Supported where?
All modern browsers and Node 14+.