Favorites

JavaScript ?. Optional chaining Operator

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
?.
JavaScript Other OperatorThis operator is supported by all modern browsers.
Updated 28th November 2026

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

const user = { name: 'Sam', address: null, greet() { return 'Hi!'; } };

console.log(user.address?.city);      // no error
console.log(user.profile?.photo?.url);
console.log(user.greet?.());
console.log(user.goodbye?.());        // method doesn't exist

const key = 'name';
console.log(user?.[key]);
console.log(user.address?.city ?? 'Unknown city');

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.

This operator is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee