Why is -1**2 a syntax error in JavaScript?

Executing it in the browser console says SyntaxError: Unexpected token **.

Because that’s the spec. Designed that way to avoid confusion about whether it’s the square of the negation of one (i.e. (-1) ** 2), or the negation of the square of one (i.e. -(1 ** 2)). This design was the result of extensive discussion of operator precedence, and examination of how this is handled in other languages, and finally the decision was made to avoid unexpected behavior by making this a syntax error.

Leave a Comment