0

I noticed that expression like "-3**2" is considered ambiguous in JavaScript. For example, in Chrome, the error is thrown as "Uncaught SyntaxError: Unary operator used immediately before exponentiation expression. Parenthesis must be used to disambiguate operator precedence."

From the perspective of compiler (specifically, parser I think) or syntax designer, why this input is not allowed? If I were the designer of the compiler, maybe I would just assign a higher precedence to unary operator, or the other way, instead of throw an error.

刘maxwell
  • 187
  • 10
  • 1
    It's not that it *couldn't be*, it's just that that was what the decided-upon syntax was - that permitting it would be too confusing for readers of the code, probably. – CertainPerformance Dec 14 '21 at 03:53
  • It would need an established precedence between the unary operator and the exponentiation operator to determine who has priority in this specific case, but as has already been said, it may have been decided that this is just an ambiguous way to write code and it's too easy to make a mistake, even if there is an actual precedence specified. So, instead, they decided to require you to specify more precisely what you want. – jfriend00 Dec 14 '21 at 03:55

0 Answers0