A brief introduction to the precedence of operators in JavaScript

  • 2020-07-21 06:52:27
  • OfStack

Operator precedence

The operator precedence in JavaScript is a set of rules. This rule controls the order in which operators are executed when an expression is evaluated. Operators of higher priority are executed before operators of lower priority. For example, multiplication is performed before addition.

The following table lists the JavaScript operators in order of highest to lowest priority. Operators of the same priority are evaluated from left to right.

运算符 描述
. [] () 字段访问、数组下标、函数调用以及表达式分组
++ -- - ~ ! delete new typeof void 1元运算符、返回数据类型、对象创建、未定义值
* / % 乘法、除法、取模
+ - + 加法、减法、字符串连接
<< >> >>> 移位
< <= > >= instanceof 小于、小于等于、大于、大于等于、instanceof
== != === !== 等于、不等于、严格相等、非严格相等
& 按位与
^ 按位异或
| 按位或
&& 逻辑与
|| 逻辑或
?: 条件
= oP= 赋值、运算赋值
, 多重求值

Parentheses can be used to change the order of evaluation determined by operator priority. This means that the expressions in parentheses should all be evaluated before they are used for the rest of the expression.


z = 78 * (96 + 3 + 45)

There are five operators in the expression: =, *, (), +, and another +. Based on the rules of operator precedence, they are evaluated in the following order: (), +, +, *, =.

Evaluate the expression in parentheses first. There are two addition operators in parentheses. Because the two addition operators have the same priority, evaluate from left to right. You add 96 to 3, and then you add the sum to 45, and you get 144.
And then multiplication. 78 times 144 is 11,232.
A ends with an assignment. Assign 11232 to z.

This is the end of this article, I hope you enjoy it.


Related articles: