Hacker News new | past | comments | ask | show | jobs | submit login
SyntaxError: cannot use `??` unparenthesized within `||` and `&&` expressions (developer.mozilla.org)
39 points by paulddraper 8 months ago | hide | past | favorite | 9 comments



This is not ambiguous, because that supposed ambiguity is used to prevent some combination of operators appearing without parentheses. Mathematically speaking the JS precedence relation is a partial order, unlike many other languages using a total order. I believe JS has made a correct choice here and a partially ordered precedence should be used more.


It is ambiguous (albeit deliberately so), therefore parens are required.

If don't believe I've used that word wrong. That's precisely the language quoted in the article for JavaScriptCore/Safari:

  SyntaxError: parentheses must be used to disambiguate


For what it's worth, the current title of this submission is "JavaScript has multiple (ambiguous) operator precedence chains", which is made up by the submitter. I question the use of a word "ambiguous" here, because it really sounds like that JS somehow had two precedence chains and had to resolve them. Absolutely not. But it's correct that the programmer intent would be indeed ambiguous, hence that error message.


Ambiguous is definitely correct. The precedence is ambiguous, but the language chooses to make that ambiguity illegal.

In some languages it might fall under the realm of "undefined behavior" (in that it compiles/validates syntactically but will likely blow up at runtime), though most linters for even those languages warn you about that. Alternatively, it might define a precedence for you, avoiding the need for parens.


Sorry to quibble about the title (here), but I thought it was going to be that which one you got was undefined, up to the interpreter, or constant but implementation defined, or something.

I suppose it's true that there are 'multiple ambiguous' chains, but it's resolved by making it a syntax error.

Maybe that's just my reading though, caffeine deficient at the moment!


The argument of the post was kind of appropriately null. Yet, I did not realize there was a ?? operator in javascript that lets you filter specifically for nulls and undefined. That was worth reading the linked article.


It was introduced in Ecmascript 2020, but many have probably already seen it in PHP (2015) or C# (since, like forever).

Or Kotlin or Objective C, though they use "?:"


Ambiguous is perhaps the wrong word here


Submitted title was "JavaScript has multiple (ambiguous) operator precedence chains". We belatedly changed it, per https://news.ycombinator.com/newsguidelines.html:

"Please use the original title, unless it is misleading or linkbait"




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: