This article briefly introduces the priority of operators in JavaScript, which is very practical. For more information, see. Operator priority
The operator priority in JavaScript is a set of rules. This rule controls the order in which operators are executed when calculating expressions. Operators with higher priority are executed before operators with lower priority. For example, multiplication is performed before addition.
The following table lists JavaScript operators based on the highest to lowest priority. Operators with the same priority are evaluated from left to right.
Operator |
Description |
. [] () |
Field access, array subscript, function call, and expression grouping |
++ ---~ ! Delete new typeof void |
Unary operator, returned data type, object creation, undefined Value |
*/% |
Multiplication, division, modulo |
+-+ |
Addition, subtraction, string connection |
<>>>> |
Shift |
<<=>> = Instanceof |
Less than, less than or equal to, greater than, greater than or equal to, instanceof |
=! ===! = |
Equal to, not equal to, strictly equal, not strictly equal |
& |
Bitwise AND |
^ |
Bitwise OR |
| |
By bit or |
&& |
Logic and |
| |
Logic or |
? : |
Condition |
= OP = |
Assignment and operation assignment |
, |
Multiple evaluate |
Parentheses can be used to change the order of evaluation determined by the operator priority. This means that the expressions in parentheses should be evaluated before they are used for the rest of the expressions.
z = 78 * (96 + 3 + 45)
There are five operators in this expression: =, *, (), +, and another +. Based on the operator priority rules, they are evaluated in the following order: (), +, +, *, =.
Evaluate the expressions in parentheses. There are two addition operators in parentheses. Because the two addition operators have the same priority, they are evaluated from left to right. Add 96 and 3 first, and then add them to 45. The result is 144.
Then there is a multiplication operation. Multiply 78 by 144, and the result is 11232.
A is the value assignment operation. Grant the value 11232 to z.
The above is all the content of this article. I hope you will like it.