JavaScript Kit > JavaScript Reference > JavaScript Operators: Precedence and Associativity
Operator precedence and associativity
Operator precedence refers to the order in which operators are evaluated when more than one type is used in an expression. The higher an operator's precedence, the earlier it is evaluated next to operators with lower precedence. For example:
a+b*c=20
The above expression is evaluated in the order of the multiplication
operators (*) first, then plus operator (+), and finally, the assignment
operator (=), due to their respective precedence order in JavaScript.
Precedence can be manually overridden using a parenthesis. The expression (a+b)*c=20
with its parenthesis around the plus
operator alters the precedence so the addition is evaluated first.
Operator associativity refers to the order in which operators with the same precedence are evaluated when they appear alongside one another. There is "left to right" and "right to left" associativity. Consider this example:
a+b-c=5
Here b is first added to a then the result subtracted by c, in that order.
Below lists the precedence (1=highest precedence, 15=lowest) and associativity order of all operators in JavaScript:
Operator precedence and associativity chart
Operator | Precedence | Associativity |
---|---|---|
|
1 | Left to right |
|
2 | Right to left |
|
3 | Right to left |
|
4 | Left to right |
|
5 | Left to right |
|
6 | Left to right |
|
7 | Left to right |
|
8 | Left to right |
|
9 | Left to right |
|
10 | Left to right |
|
11 | Left to right |
|
12 | Right to left |
|
13 | Right to left |
|
14 | Right to left |
|
15 | Left to right |
- JavaScript Operators
- JavaScript Statements
- Global functions
- JavaScript Events
- Escape Sequences
- Reserved Words