Optional chaining (?.) - JavaScript | MDN

developer.mozilla.org

Tin mới

HTML: Markup language

See all HTML references

Date & time formats

See all HTML guides

CSS: Styling language

See all CSS references

See all CSS guides

Centering an element

JavaScript: Scripting language

Standard built-in objects

Expressions & operators

Statements & declarations

See all JavaScript references

Control flow & error handing

Loops and iteration

Working with objects

See all JavaScript guides

Web APIs: Programming interfaces

Service worker API

Using the Web animation API

Using the Fetch API

Working with the History API

Using the Web speech API

All web technology

Progressive web apps

Learn web development

Getting started modules

Structuring content with HTML module

CSS styling basics module

Dynamic scripting with JavaScript module

Border-image generator

Border-radius generator

Box-shadow generator

Color format converter

Optional chaining with function calls

You can use optional chaining when attempting to call a method which may not exist. This can be helpful, for example, when using an API in which a method might be unavailable, either due to the age of the implementation

Optional chaining with function calls

You can use optional chaining when attempting to call a method which may not exist. This can be helpful, for example, when using an API in which a method might be unavailable, either due to the age of the implementation

Optional chaining with expressions

You can also use the optional chaining operator with bracket notation, which allows passing an expression as the property name:

Template literal tags

It is invalid to try to assign to the result of an optional chaining expression:

SyntaxError: tagged template cannot be used with optional chain

It is invalid to try to assign to the result of an optional chaining expression:

Invalid optional chaining

It is invalid to try to assign to the result of an optional chaining expression:

SyntaxError: new keyword cannot be used with an optional chain

It is invalid to try to assign to the result of an optional chaining expression:

Dealing with optional callbacks or event handlers

If you use callbacks or fetch methods from an object with a destructuring pattern, you may have non-existent values that you cannot call as functions unless you have tested their existence. Using ?., you can avoid this e

nullish coalescing operator

The nullish coalescing operator may be used after optional chaining in order to build a default value when none was found:

Learn how to contribute

This page was last modified on May 22, 2026 by MDN contributors.

Expressions and operators

Numbers and strings

Representing dates & times

Regular expressions

Indexed collections

JavaScript typed arrays