为什么-1**2在JavaScript中是一个语法错误?

时间:2022-09-14 23:02:23

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

在浏览器控制台执行该操作时,它表示SyntaxError:意外的令牌**。在节点:

> -1**2
...
...
...
...^C

I thought this is an arithmetic expression where ** is the power operator. There is no such issue with other operators.

我认为这是一个算术表达式,其中**是幂运算符。其他运营商没有这样的问题。

Strangely, typing */ on the second line triggers the execution:

奇怪的是,在第二行输入*/会触发执行:

> -1**2
... */
-1**2
  ^^
SyntaxError: Unexpected token **

What is happening here?

这里正在发生什么?

2 个解决方案

#1


77  

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

在浏览器控制台中执行它会说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.

因为这是规范。设计这种方式是为了避免混淆它是1的否定的平方(即(-1)* 2),还是1的平方(即-(1 * 2))的否定。这个设计是对运算符优先级的广泛讨论的结果,以及对如何在其他语言中处理它的检查,最后决定通过将此设置为语法错误来避免意外行为。

#2


39  

From the documentation on MDN:

从关于MDN的文件来看:

In JavaScript, it is impossible to write an ambiguous exponentiation expression, i.e. you cannot put a unary operator (+/-/~/!/delete/void/typeof) immediately before the base number.

在JavaScript中,不可能编写一个不明确的指数表达式,也就是说,不能在基数之前加上一个一元运算符(+/-/~/!/delete/void/typeof)。

The reason is also explained in that same text:

原因也在同一文本中得到解释:

In most languages like PHP and Python and others that have an exponentiation operator (typically ^ or **), the exponentiation operator is defined to have a higher precedence than unary operators such as unary + and unary -, but there are a few exceptions. For example, in Bash the ** operator is defined to have a lower precedence than unary operators.

在大多数语言PHP,Python和其他有求幂运算符(通常^或者* *),求幂运算符被定义为有更高的优先级比一元等一元运算符+和一元,但也有少数例外。例如,在Bash中定义**运算符的优先级比一元运算符低。

So to avoid confusion it was decided that the code must remove the ambiguity and explicitly put the parentheses:

为了避免混淆,我们决定代码必须消除歧义并明确地把括号括起来:

(-1)**2

or:

或者:

-(1**2) 

As a side note, the binary - is not treated that way -- having lower precedence -- and so the last expression has the same result as this valid expression:

作为补充说明,二进制的-不是那样处理的-优先级较低-所以最后一个表达式的结果与这个有效表达式相同:

0-1**2

Exponentiation Precedence in Other Programming Languages

As already affirmed in above quote, most programming languages that have an infix exponentiation operator, give a higher precedence to that operator than to the unary minus.

如上所述,大多数具有中缀指数运算符的编程语言对该运算符的优先级都高于一元减号。

Here are some other examples of programming languages that give a higher precedence to the unary minus operator:

下面是一些编程语言的其他例子,它们优先于一元负运算符:

#1


77  

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

在浏览器控制台中执行它会说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.

因为这是规范。设计这种方式是为了避免混淆它是1的否定的平方(即(-1)* 2),还是1的平方(即-(1 * 2))的否定。这个设计是对运算符优先级的广泛讨论的结果,以及对如何在其他语言中处理它的检查,最后决定通过将此设置为语法错误来避免意外行为。

#2


39  

From the documentation on MDN:

从关于MDN的文件来看:

In JavaScript, it is impossible to write an ambiguous exponentiation expression, i.e. you cannot put a unary operator (+/-/~/!/delete/void/typeof) immediately before the base number.

在JavaScript中,不可能编写一个不明确的指数表达式,也就是说,不能在基数之前加上一个一元运算符(+/-/~/!/delete/void/typeof)。

The reason is also explained in that same text:

原因也在同一文本中得到解释:

In most languages like PHP and Python and others that have an exponentiation operator (typically ^ or **), the exponentiation operator is defined to have a higher precedence than unary operators such as unary + and unary -, but there are a few exceptions. For example, in Bash the ** operator is defined to have a lower precedence than unary operators.

在大多数语言PHP,Python和其他有求幂运算符(通常^或者* *),求幂运算符被定义为有更高的优先级比一元等一元运算符+和一元,但也有少数例外。例如,在Bash中定义**运算符的优先级比一元运算符低。

So to avoid confusion it was decided that the code must remove the ambiguity and explicitly put the parentheses:

为了避免混淆,我们决定代码必须消除歧义并明确地把括号括起来:

(-1)**2

or:

或者:

-(1**2) 

As a side note, the binary - is not treated that way -- having lower precedence -- and so the last expression has the same result as this valid expression:

作为补充说明,二进制的-不是那样处理的-优先级较低-所以最后一个表达式的结果与这个有效表达式相同:

0-1**2

Exponentiation Precedence in Other Programming Languages

As already affirmed in above quote, most programming languages that have an infix exponentiation operator, give a higher precedence to that operator than to the unary minus.

如上所述,大多数具有中缀指数运算符的编程语言对该运算符的优先级都高于一元减号。

Here are some other examples of programming languages that give a higher precedence to the unary minus operator:

下面是一些编程语言的其他例子,它们优先于一元负运算符: