Categories:

JavaScript Kit > JavaScript Reference > JavaScript Operators: Precedence and Associativity

Operator precedence and associativity

Last updated: March 23rd, 2008

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
  • . (dot property reference)
  • [] (bracket property reference)
  • new
  • ()
1 Left to right
  • ++
  • --
2 Right to left
  • + (unary +, NOT the addition sign)
  • - (unary negate -, NOT subtract sign)
  • ~ (bitwise not)
  • ! (not)
  • delete
  • typeof
  • void
3 Right to left
  • *
  • /
  • %
  • + (addition)
  • - (subtraction)
4 Left to right
  • <<
  • >>
  • >>>
5 Left to right
  • <
  • <=
  • >
  • >=
  • instanceof
  • in
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
  • , (comma)
15 Left to right

Reference List

Partners
Right column

CopyRight 1998-2012 JavaScript Kit. NO PART may be reproduced without author's permission.