Operator Precedence

Table of contents

  1. 1. Summary
  2. 2. Associativity
  3. 3. Table
Table of contents
  1. 1. Summary
  2. 2. Associativity
  3. 3. Table

 

Summary

Operator precedence determines the order in which operators are evaluated. Operators with higher precedence are evaluated first.

A common example:

3 + 4 * 5 // returns 23	 

The multiplication operator ("*") has higher precedence than the addition operator ("+") and thus will be evaluated first.

Associativity

Associativity determines the order in which operators of the same precedence are processed. For example, consider an expression:

a OP b OP c

Left-associativity (left-to-right) means that it is processed as (a OP b) OP c, while right-associativity (right-to-left) means it is interpreted as a OP (b OP c). Assignment operators are right-associative, so you can write:

a = b = 5;

with the expected result that a and b get the value 5. This is because the assignment operator returns the value that it assigned. First, b is set to 5. Then the a is set to the value of b.

Table

The following table is ordered from highest (1) to lowest (17) precedence.

Precedence Operator type Associativity Individual operators
1 member left-to-right .
[]
new right-to-left new
2 function call left-to-right ()
3 increment n/a ++
decrement n/a --
4 logical-not right-to-left !
bitwise not right-to-left ~
unary + right-to-left +
unary negation right-to-left -
typeof right-to-left typeof
void right-to-left void
delete right-to-left delete
5 multiplication left-to-right *
division left-to-right /
modulus left-to-right %
6 addition left-to-right +
subtraction left-to-right -
7 bitwise shift left-to-right <<
>>
>>>
8 relational left-to-right <
<=
>
>=
in left-to-right in
instanceof left-to-right instanceof
9 equality left-to-right ==
!=
===
!==
10 bitwise-and left-to-right &
11 bitwise-xor left-to-right ^
12 bitwise-or left-to-right |
13 logical-and left-to-right &&
14 logical-or left-to-right ||
15 conditional right-to-left ?:
16 assignment right-to-left =
+=
-=
*=
/=
%=
<<=
>>=
>>>=
&=
^=
|=
17 comma left-to-right ,

Tags (0)

Attachments (0)

 

Attach file