English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The precedence of operators determines how they parse each other.
The operator with higher precedence will become the operand of the operator with lower precedence.
In the following example, the precedence of multiplication is higher than that of addition, which determines the result of the operation.
// First, add3Multiply5, then add10 var x = 10 + 3 * 5;Test to see‹/›
On the contrary, if we want to run the addition operation first, we should group it in parentheses, which always has the highest precedence.
// First, add10And3Add, then multiply5 var x = (10 + 3) * 5;Test to see‹/›
When using parentheses, the operations inside the parentheses should be calculated first.
When many operations have the same precedence (for example, addition and subtraction), they are calculated from left to right:
var x = 10 + 3 - 5;Test to see‹/›
The evaluation of expressions is also affected by the associativity of operators.
Associativity refers to the direction in which the entire expression is evaluated (from right to left or from left to right).
If two or more operators with the same precedence appear in an expression, which operator will be calculated first? The associativity of operators answers this question.
Please refer to the table below to resolve any related or precedence issues in JavaScript
Operator | Usage Description | Operator Association | Operator Precedence |
---|---|---|---|
() | Method/Function Call, Grouping | Left to Right | Maximum- 1 |
[] | Array Access | Left to Right | 1 |
. | Object Property Access | Left to Right | 1 |
++ | Increment | Right to Left | 2 |
-- | Decrement | Right to Left | 2 |
- | Subtraction | Right to Left | 2 |
! | Logical NOT | Right to Left | 2 |
~ | Bitwise NOT | Right to Left | 2 |
delete | Delete array values or object properties | Right to Left | 2 |
new | Create an object | Right to Left | 2 |
typeof | Return Data Type | Right to Left | 2 |
void | Do not specify the value to be returned | Right to Left | 2 |
/ | Division | Left to Right | 3 |
* | Multiplication | Left to Right | 3 |
% | Modulus | Left to Right | 3 |
+ | Addition | Left to Right | 4 |
+ | String Concatenation | Left to Right | 4 |
- | Subtraction | Left to Right | 4 |
>> | Bitwise Right Shift | Left to Right | 5 |
<< | Bitwise Left Shift | Left to Right | 5 |
>, >= | Greater Than, Greater Than or Equal | Left to Right | 6 |
<, <= | Less Than, Less Than or Equal | Left to Right | 6 |
== | Equal | Left to Right | 7 |
!= | Not Equal | Left to Right | 7 |
=== | Consistent/Strictly Equal (Same data types) | Left to Right | 7 |
!== | Not Consistent/Strictly Not Equal (Different data types) | Left to Right | 7 |
& | Bitwise AND | Left to Right | 8 |
^ | Bitwise XOR | Left to Right | 9 |
| | Bitwise OR | Left to Right | 10 |
&& | Logical AND | Left to Right | 11 |
|| | Logical OR | Left to Right | 12 |
?: | Conditional Branch | Left to Right | 13 |
= | Assignment | Right to Left | 14 |
*=, /=, %=, +=,, -=, <<=, >>=, >>>=, &=, ^=, |= | Allocation according to the preceding operator | Right to Left | 14 |
, | Multiple Evaluation | Left to Right | Minimum:15 |