The operator precedence along with their associativity determines how JavaScript evaluates an expression when there are multiple JavaScript Operators present in the expression. Each JavaScript operators have certain precedence. The JavaScript evaluates the operators with higher precedence first. If a group of operators has the same Precedence, then it evaluates them either left to right or right to left, depending on the operator’s associativity. The table end of the article. has a list of all operators, with their precedence along with associativity.
For Example, take a simple arithmetic operation.
100+100*20
If we did the math from left to right and without any precedence we would add 100+100=200, and then multiply it by 20. The answer would be 4000. But that is not what we get.
Multiplication always has higher precedence than an addition. Hence it performs the multiplication first (100*20=2000) and then the addition (100+2000=2100)
console.log((100+100)*20); //4000
The associativity defines the direction in which the evaluation takes place. It can be either left to right (left-associativity) or right to left (right associativity).
The assignment Operator = has a right to left associativity.
y=5
x = y = 10
TNote that in the above example if = had left to right associativity, then the value of x will be 5.
Multiplication, division & reminder has the same Precedence and left-to-right associativity. The expression 10/5*20/8 is evaluated in the following order.
The Parenthesis or grouping operator has the highest precedence. However, even they might not be evaluated, when they are part of the expression involving short-circuited Operators.
The Logical operators like || & && stop the evaluation, if they find the match This is known as a short-circuiting. For Example, the code below does not invoke alert("Hello") even with a parenthesis.
Similarly in expression, a || (b * c) b*c never invoked if a is truthy.
console.log(true || (alert("Hello")))
//alert is never evaluated
let a =10
let b = 5
let c = 5
console.log(a || (b * c)); //10 b*c never invoked
The logical AND, nullish-coalescing, optional chaining, and conditional operator are the other short-circuited operators.
| Precedence | Operator type | Associativity | Individual operators |
|---|---|---|---|
| 21 | Grouping | n/a | ( … ) |
| 20 | Member Access | left-to-right | … . … |
| Computed Member Access | left-to-right | … [ … ] | |
| new (with argument list) | n/a | new … ( … ) | |
| Function Call | left-to-right | … ( … ) | |
| Optional chaining | left-to-right | ?. | |
| 19 | new (without argument list) | right-to-left | new … |
| 18 | Postfix Increment | n/a | … ++ |
| Postfix Decrement | … -- | ||
| 17 | Logical NOT | right-to-left | ! … |
| Bitwise NOT | ~ … | ||
| Unary Plus | + … | ||
| Unary Negation | - … | ||
| Prefix Increment | ++ … | ||
| Prefix Decrement | -- … | ||
| typeof | typeof … | ||
| void | void … | ||
| delete | delete … | ||
| await | await … | ||
| 16 | Exponentiation | right-to-left | … ** … |
| 15 | Multiplication | left-to-right | … * … |
| Division | … / … | ||
| Remainder | … % … | ||
| 14 | Addition | left-to-right | … + … |
| Subtraction | … - … | ||
| 13 | Bitwise Left Shift | left-to-right | … << … |
| Bitwise Right Shift | … >> … | ||
| Bitwise Unsigned Right Shift | … >>> … | ||
| 12 | Less Than | left-to-right | … < … |
| Less Than Or Equal | … <= … | ||
| Greater Than | … > … | ||
| Greater Than Or Equal | … >= … | ||
| in | … in … | ||
| instanceof | … instanceof … | ||
| 11 | Equality | left-to-right | … == … |
| Inequality | … != … | ||
| Strict Equality | … === … | ||
| Strict Inequality | … !== … | ||
| 10 | Bitwise AND | left-to-right | … & … |
| 9 | Bitwise XOR | left-to-right | … ^ … |
| 8 | Bitwise OR | left-to-right | … | … |
| 7 | Logical AND | left-to-right | … && … |
| 6 | Logical OR | left-to-right | … || … |
| 5 | Nullish coalescing operator | left-to-right | … ?? … |
| 4 | Conditional | left-to-right | … ? … : … |
| 3 | Assignment | right-to-left | … = … |
| … += … | |||
| … -= … | |||
| … **= … | |||
| … *= … | |||
| … /= … | |||
| … %= … | |||
| … <<= … | |||
| … >>= … | |||
| … >>>= … | |||
| … &= … | |||
| … ^= … | |||
| … |= … | |||
| … &&= … | |||
| … ||= … | |||
| … ??= … | |||
| 2 | yield | right-to-left | yield … |
| yield* | yield* … | ||
| 1 | Comma / Sequence | left-to-right | … , … |
Read More