When I use the exponent operator (**
) in JavaScript, it normally works as expected:
2 ** 2 // 4
2 ** -2 // 0.25
But when the left operand is negative
-2 ** 2
I get a syntax error:
Uncaught SyntaxError: Unexpected token **
I can get around it easily by putting parentheses around -2
(-2) ** 2 // 4
but I'm curious about what caused this error. Other operators (+ - * / %
etc) don't have this problem. Why does this happen for the **
operator?