Using optional chaining operator for object property access

When accessing a property using bracket notation and optional chaining, you need to use a dot in addition to the brackets:

const value = a?.[b]?.c;

This is the syntax that was adopted by the TC39 proposal, because otherwise it’s hard for the parser to figure out if this ? is part of a ternary expression or part of optional chaining.

The way I think about it: the symbol for optional chaining isn’t ?, it’s ?.. If you’re doing optional chaining, you’ll always be using both characters.

Leave a Comment