JavaScript ?. Optional chaining Operator
The optional chaining operator (?.) reads a property or calls a method only if the value before it is not null or undefined. Otherwise it returns undefined instead of throwing an error.
Description
The optional chaining operator ?. accesses a property or calls a function, but stops early and returns undefined if the value on its left is null or undefined. Without it, reading user.address.city when address is missing throws a TypeError.
It has three forms: obj?.prop for properties, obj?.[key] for computed properties, and func?.() to call a function only if it exists. When the chain stops, nothing to its right is evaluated, so side effects such as function calls in the rest of the expression don't happen.
It pairs well with the nullish coalescing operator ?? to supply a default, as in user?.name ?? 'Guest'. Use it for values that can legitimately be missing; overusing it can hide bugs where a value should always exist. You can't assign to an optional chain, so obj?.prop = 1 is a syntax error.
Properties
- Operator
?.- Category
- Other
- Precedence
- 17 (1 is lowest, 18 is highest)
- Associativity
- left-to-right
Syntax
obj?.prop
obj?.[expression]
func?.(args)Example
Browser Support
The following information will show you the current browser support for the ?. operator. Hover over a browser icon to see the version that first introduced support for this operator.
Desktop





Tablets & Mobile





