The Optional Chaining Operator in JavaScript

Miguel Angel Muñoz
Version 1
Published in
1 min readAug 31, 2022
Photo by Aida L on Unsplash

The optional chaining operator (?.) enables you to read the value of a property located deep within a chain of connected objects without checking if each reference in the chain is nullish (null or undefined).

The ?. operator is like the . chaining operator, except that instead of causing an error if a reference is nullish (null or undefined), the expression short-circuits with a return value of undefined. When used with function calls, it returns undefined if the given function does not exist.

This helps you write simple and readable code, avoiding long conditional checks.

Remember that a nullish value and a falsy value are different concepts. While null, undefined, 0, '', false, and NaN are considered falsy values, only null and undefined are considered nullish values.

Check the following code snippet:

Check browser compatibility: https://caniuse.com/mdn-javascript_operators_optional_chaining

About the author:

Miguel Munoz is a Principal Software Engineer here at Version 1.

--

--

Miguel Angel Muñoz
Version 1

Principal Software Engineer @ Version 1. Front-end/Javascript enthusiast 💻 & Video Game lover 🎮 . I spend my free time learning 📝 or playing 👾.