JavaScript Operator Precedence
- Previous Page JS Regular Expressions
- Next Page JS Exceptions
Operator precedence describes the order of execution of operations in arithmetic expressions.
The precedence of multiplication (*) and division (/) is higher than that of addition (+) and subtraction (-).
Example
As with traditional mathematics, multiplication is performed first:
let x = 100 + 50 * 3;
When using parentheses, the operations within the parentheses are calculated first:
let x = (100 + 50) * 3;
Operations with the same precedence (such as * and /) are calculated from left to right:
let x = 100 / 50 * 3;
Operator precedence values
Expressions within parentheses are evaluated before the rest of the expression, and functions are executed before the result is used in the rest of the expression:
Precedence | Operator | Description | Example |
---|---|---|---|
18 | ( ) | Expression grouping | (100 + 50) * 3 |
myFunction() | . | [] | . |
myFunction() | person.name | [] | Property membership |
myFunction() | person["name"] | ?. ES2020 | Optional chaining |
myFunction() | x ?. y | () | Function call |
myFunction() | 16 | 17 | New with parameters |
new Date("June 5,2022") | 16 | new | New without parameters |
new Date()Increment operators |
|||
i++ | ++ | Postfix increment is executed before prefix increment. | Postfix increment |
i++ | -- | 15 | i-- |
14 | ++ | Prefix increment | ++i |
14 | -- | Prefix decrement | --i |
NOT operator |
|||
14 | ! | Logical NOT | !(x==y) |
14 | ~ | Bitwise NOT | ~x |
Unary operators |
|||
14 | + | Unary plus | +x |
14 | - | Unary minus | -x |
14 | typeof | Data type | typeof x |
14 | void | Evaluate empty | void(0) |
14 | delete | Property deletion | delete myCar.color |
Arithmetic operatorsExponential operation is executed before multiplication. Multiplication and division are executed before addition and subtraction. |
|||
13 | ** | Exponential operation ES2016 | 10 ** 2 |
12 | * | Multiplication | 10 * 5 |
12 | / | Division | 10 / 5 |
12 | % | Division remainder | 10 % 5 |
11 | + | Addition | 10 + 5 |
11 | - | Subtraction | 10 - 5 |
11 | + | Concatenation | "Bill" + "Gates" |
Shift operators |
|||
10 | << | Left shift | x << 2 |
10 | >> | Right shift (signed) | x >> 2 |
10 | >>> | Right shift (unsigned) | x >>> 2 |
Relational operators |
|||
9 | in | Property in object | "PI" in Math |
9 | instanceof | Object instance | x instanceof Array |
Comparison operators |
|||
9 | < | Less than | x < y |
9 | <= | Less than or equal to | x <= y |
9 | > | Greater than | x > y |
9 | >= | Greater than or equal to | x >= Array |
8 | == | Equal to | x == y |
8 | === | Strict equality | x === y |
8 | != | Inequality | x != y |
8 | !== | Strict inequality | x !== y |
Bitwise operators |
|||
7 | & | Bitwise AND | x & y |
6 | ^ | Bitwise XOR | x ^ y |
5 | | | Bitwise OR | x | y |
Logical operators |
|||
4 | && | Logical AND | x && y |
3 | || | Logical OR | x || y |
3 | ?? | Nullish coalescing ES2020 | x ?? y |
Conditional (ternary) operator |
|||
2 | ? : | Conditional | ? "yes" : "no" |
Assignment OperatorAssignment is executed after other operations. |
|||
2 | = | Simple Assignment | x = y |
2 | : | Colon Assignment | x: 5 |
2 | += | Addition Assignment | x += y |
2 | -= | Subtraction Assignment | x -= y |
2 | *= | Multiplication Assignment | x *= y |
2 | **= | Exponent Assignment | x **= y |
2 | /= | Division Assignment | x /= y |
2 | %= | Modulo Assignment | x %= y |
2 | <<= | Left Shift Assignment | x <<= y |
2 | >>= | Right Shift Assignment | x >>= y |
2 | >>>= | Unsigned Right Shift | x >>>= y |
2 | &= | Bitwise AND Assignment | x &= y |
2 | |= | Bitwise OR Assignment | x |= y |
2 | ^= | Bitwise XOR Assignment | x ^= y |
2 | &&= | Logical AND Assignment | x &= y |
2 | ||= | Logical OR Assignment | x ||= y |
2 | => | Arrow | x => y |
2 | yield | Pause / Resume | yield x |
2 | yield* | Delegation Operator | yield* x |
2 | ... | Spread Operator | ... x |
1 | , | Comma | x, y |
- Previous Page JS Regular Expressions
- Next Page JS Exceptions