Optional chaining - the "syntax" or the "operator"?

Should we refer to the optional chaining a?.b as the "syntax" or the "operator"?

The proposal suggests that it's the operator, but the spec seems vague about it.

1 Like


In order to say definitely whether or not a thing is an “operator”, you must have first a definite definition of what an “operator” is. However, neither the proposal, nor the spec define the notion of “operator”; rather, they follow each some usage, and usage may differ among documents.

As main editor of the proposal, when I wrote its explainer, I was obviously influenced by documentation about the feature in other languages. Most of them use the term “operator”; therefore that word has appeared in my prose, although I’ve not specifically sought to use it.

But when I wrote the formal spec for inclusion in ECMA-262, I’ve followed the preëxisting language conventions that were used for features that was syntactically and semantically close to the new one, namely, property access and function/method invocation. The term “operator” was not used to describe those features, therefore it hasn’t been used either for the new one.

@claudepache I'm very glad to get the answer from a person who really knows the topic.

As the author of the Modern JavaScript Tutorial https://javascript.info, I'm very concerned with correctness of what I tell people.

Right now I'm using the term "optional chaining syntax" in the tutorial. But some other sources use the term "optional chaining operator". People ask me, what's right?

And here seems to be the "last resort" place to seek the ultimate answer.

Please explain. Let's settle this =)

This is mostly a matter of style, that I won’t settle for you. But here is my personal tendency: I think of optional chaining as a feature of the language, not as an operator, and as such, I just say “optional chaining”.

You can look at the Swift documentation about the topic for an example of discussion using neither the term “syntax”, nor “operator”. (This is where I’ve stolen the name “optional chaining”. Of course, that description, does not translate readily to JS, because we don’t have any notion of “optional”.)

Here is a short description of the feature in JS, the way I think of it. The token ?. is the concrete syntax used for optional chaining. I haven’t any proper name for that specific token, sorry. You can call it “optional chaining operator” if you think of “.” as the “property access operator”, but personally I tend to avoid to call any of them “operator”. That token is placed at the beginning of a chain (possibly of length one) of property accesses and function/method calls. (In the spec, such a chain (including the introducing ?. token) is called an “optional chain”, because it was convenient to name it.) If the expression found at the LHS of the question mark evaluates to a non-nullish value, then the chain is evaluated normally; otherwise it is skipped and the entire expression evaluates to undefined.


Leaving "syntax" then ;) Thank you!