Exponentiation operator precedence

Jason Orendorff jason.orendorff at gmail.com
Mon Aug 24 17:08:57 UTC 2015

In math, -x² is -(x²), not (-x)². But as proposed for JS, -x**2 is (-x)**2.

PHP, Python, Haskell, and D side with the traditional algebraic
notation, against JS. Here's PHP:

    $ php -r 'print(-2 ** 2);'


    >>> -2 ** 2


    Prelude> -2 ^ 2

The D grammar: http://dlang.org/grammar.html#UnaryExpression

Let's switch.

Another case to think about is `-2 ** -2`. In Haskell, that's a syntax
error, but the other three languages all treat it the same way, so
maybe JS should follow suit.


More information about the es-discuss mailing list