A mantra in the Zig community that is applicable here: "Handled errors are better than exceptions, but exceptions are better than bugs" Answer from tony_bradley91 on reddit.com
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Optional_chaining
Optional chaining (?.) - JavaScript | MDN
The optional chaining (?.) operator accesses an object's property or calls a function. If the object accessed or function called using this operator is undefined or null, the expression short circuits and evaluates to undefined instead of throwing an error.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › objects: the basics
Optional chaining '?.'
The optional chaining ?. is a safe way to access nested object properties, even if an intermediate property doesn’t exist. If you’ve just started to read the tutorial and learn JavaScript, maybe the problem hasn’t touched you yet, but it’s quite common.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
What Is the Optional Chaining Operator, and How Does It Work? - JavaScript - The freeCodeCamp Forum
April 8, 2025 - TLDR: Optional Chaining Operator is for objects, not properties? https://www.w3schools.com/jS/js_2020.asp " The Optional Chaining Operator returns undefined if an object is undefined or null (instead of throwing an er…
🌐
DEV Community
dev.to › tusharshahi › cost-of-unnecessary-optional-chaining-and-nullish-coalescing-operator-3h09
Cost of unnecessary Optional Chaining (and Nullish coalescing operator) - DEV Community
April 6, 2024 - If you are a developer who has worked with JavaScript in recent years, I am sure you are well aware of the below syntax: ... We see them all around us, making our lives easier. The first one is optional chaining and the other one is the nullish coalescing operator.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-optional-chaining
JavaScript Optional Chaining - GeeksforGeeks
JavaScript Optional Chaining (ES2020) simplifies safe access to deeply nested object properties by preventing errors when values are null or undefined.
Published   January 12, 2026
🌐
Reddit
reddit.com › r/javascript › [askjs] over-using optional chaining. is this pattern common?
r/javascript on Reddit: [AskJS] Over-using optional chaining. Is this pattern common?
January 18, 2021 -

Hi everyone!

I love optional chaining, i really do, but there are some cases where using this syntax damages the readability of the code. One of those cases is the following

function optionalFunction(){     
    console.log("works"); 
}  
// optionalFunction = undefined;  

optionalFunction?.(); 

While i understand this approach, i find it optionalFunction?.() harder to read as opposed to this

function optionalFunction(){     
    console.log("works"); 
}  
// optionalFunction = undefined;  

if(optionalFunction != undefined){     
    optionalFunction(); 
} 

I think i'd rather have a more readable and stronger check than ES6 magic when checking if an optional function is defined.

I believe that optional chaining fixes the problem of checking if a property of an object exists, and if exists, then get the value or keep going deeper in the object structure. But this syntax just looks weird for calling functions, it looks a lot like those "one line cleverness" code that sometimes people encounter.

What are your thoughts about this?

Find elsewhere
🌐
Can I Use
caniuse.com › mdn-javascript_operators_optional_chaining
JavaScript operator: Optional chaining operator (`?.`) | Can I use... Support tables for HTML5, CSS3, etc
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
Top answer
1 of 7
22

This is currently a Stage 4 proposal you can check on the progress of it here:
https://github.com/tc39/proposal-optional-chaining

You can use the babel plugin today:
https://www.npmjs.com/package/babel-plugin-transform-optional-chaining

Update 11th January 2020: Babel now supports optional chaining by default https://babeljs.io/blog/2020/01/11/7.8.0

The Optional Chaining operator is spelled ?.. It may appear in three positions:

obj?.prop       // optional static property access
obj?.[expr]     // optional dynamic property access
func?.(...args) // optional function or method call

Notes:

In order to allow foo?.3:0 to be parsed as foo ? .3 : 0 (as required for backward compatibility), a simple lookahead is added at the level of the lexical grammar, so that the sequence of characters ?. is not interpreted as a single token in that situation (the ?. token must not be immediately followed by a decimal digit).

Also worth checking out:

https://github.com/tc39/proposal-nullish-coalescing

https://github.com/babel/babel/tree/master/packages/babel-plugin-proposal-nullish-coalescing-operator

2 of 7
7

In plain JavaScript you have to do type checks or structure your code so that you know an object will exist.

CoffeeScript, a language that compiles down to JavaScript, provides an existential operator ?. for safe chaining if you're willing to consider a preprocessed language.

There's another discussion here about why you can't reproduce this behavior in JS.

There is also a discussion on the ESDiscuss forums about adding an existential operator to a future version of JavaScript. It doesn't seem very far along though, certainly nowhere close to practical use. More of an idea at this point.

Top answer
1 of 5
589

You need to put a . after the ? to use optional chaining:

myArray.filter(x => x.testKey === myTestKey)?.[0]

Playground link

Using just the ? alone makes the compiler think you're trying to use the conditional operator (and then it throws an error since it doesn't see a : later)

Optional chaining isn't just a TypeScript thing - it is a finished proposal in plain JavaScript too.

It can be used with bracket notation like above, but it can also be used with dot notation property access:

const obj = {
  prop2: {
    nested2: 'val2'
  }
};

console.log(
  obj.prop1?.nested1,
  obj.prop2?.nested2
);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

And with function calls:

const obj = {
  fn2: () => console.log('fn2 running')
};

obj.fn1?.();
obj.fn2?.();
Run code snippetEdit code snippet Hide Results Copy to answer Expand

2 of 5
50

Just found it after a little searching on the what's new page on official documentation

The right way to do it with array is to add . after ?

so it'll be like

myArray.filter(x => x.testKey === myTestKey)?.[0] // in case of object
x?.() // in case of function

I'll like to throw some more light on what exactly happens with my above question case.

myArray.filter(x => x.testKey === myTestKey)?[0]

Transpiles to

const result = myArray.filter(x => x.testKey === myTestKey) ? [0] : ;

Due to which it throws the error since there's something missing after : and you probably don't want your code to be transpilled to this.

Thanks to Certain Performance's answer I learned new things about typescript especially the tool https://www.typescriptlang.org/play/index.html .

🌐
DEV Community
dev.to › mnathani › no-unsafe-optional-chaining-is-crazy--4icm
JavaScript's unsafe optional chaining is crazy |: - DEV Community
December 3, 2022 - ~ is a bitwise NOT operator - flipping all the bits in a number. Flipping them twice gets you back to the original number. Using it with undefined (what you'll get from the optional chain if order is missing) or any falsy value - is the same as using it with 0
🌐
Medium
medium.com › @AlexanderObregon › optional-chaining-in-javascript-works-differently-than-you-think-216a8a43e45b
Optional Chaining in JavaScript Works Differently Than You Think
May 25, 2025 - Optional chaining gives you a way to safely walk through an object’s structure without crashing the program when something in the chain is missing. The idea is simple: stop early and return undefined if the value you're looking for doesn’t exist. But this isn’t just a neat shortcut. JavaScript treats the ?.
🌐
Medium
medium.com › @dave_lunny › why-im-so-jazzed-about-the-optional-chaining-proposal-in-javascript-23309320532
Why I’m so jazzed about the Optional Chaining proposal in JavaScript 🎷 | by Dave Lunny | Medium
July 25, 2019 - Some folks/teams prefer to play it safe by only using proposals which are probably going to actually make it into the language (Stage 3 or 4). This keeps source code closer to “real” JavaScript, and helps avoid the need for refactors later if the spec changes, as well as to protect against unstable APIs which haven’t yet been tested in the wild. Generally speaking though, once a proposal makes it to Stage 2, it’s probably in pretty good shape, and will likely eventually be included in the language. Optional Chaining is at Stage 2 now, so I think it’s time to discuss it.
🌐
V8
v8.dev › features › optional-chaining
Optional chaining · V8
August 27, 2019 - What happens when db, user, or name is undefined or null? With the optional chaining operator, JavaScript initializes nameLength to undefined instead of throwing an error.
🌐
DEV Community
dev.to › jesterxl › avoid-optional-chaining-and-optional-properties-5d4b
Avoid Optional Chaining and Optional Properties - DEV Community
April 21, 2025 - It’s clear many JavaScript/TypeScript developers who utilize optional chaining like “thing | undefined”, or worse optional properties, “property?: thing | undefined” haven’t learned this painful lesson. It may look flexible, having multiple classes/functions capable of handling ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Errors › Bad_new_optional
SyntaxError: new keyword cannot be used with an optional chain - JavaScript | MDN
July 8, 2025 - Optional new is specifically forbidden because its syntax is complicated (new with and without arguments), and the result is unclear (it would be the only case where new does not evaluate to an object value). You need to translate the optional chaining to its underlying condition (see optional chaining for more information).
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › javascript optional chaining operator
JavaScript Optional Chaining Operator (?.)
December 17, 2023 - The optional chaining operator (?.) is like a shortcut for accessing nested properties in a series of objects. Instead of having to check if each step in the chain is empty (null or undefined), you can use the operator ?.
🌐
freeCodeCamp
freecodecamp.org › news › optional-chaining-javascript
Optional Chaining in JavaScript – Explained with Examples
February 13, 2024 - The optional chaining operator (?.) allows you to access properties or methods without the need for explicit null or undefined checks.
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_optional_chaining.htm
JavaScript - Optional Chaining
The optional chaining in JavaScript allows you to access nested properties and methods of an object without checking if each property exists. This can help to make your code more concise and easier to read.