Skip to main content Accessibility Feedback

Objects

Optional Chaining

Optional chaining is a browser-native way to chain methods or properties, and conditionally continue down the chain only if the value is not null or undefined.

To use optional chaining, add a question mark (?) before the dot (.) in your chain.

// returns "Abracadabra!"
let summonOptional = wizard?.spells?.summon?.phrase;

// returns undefined but does not throw an error
let teacupsOptional = wizard?.spells?.teacup?.phrase;

Preorder my new course on Web Components! Want to learn how to build Web Components from scratch, master best practices, and more? Preorder today and get $100 off of the launch price.


Find this useful? You can support my work by purchasing an annual membership.