/

HTML / CSS / JavaScript Tutorial

JavaScript Operator Precedence

[this page | pdf | back links]

JavaScript adopts the following operator precedence (which is broadly in line with most other main-stream object orientated programming languages). In this list earlier means higher precedence (i.e. applied before later operators on the list), with operators with equal precedence grouped together:

 

Precedence Level

Operator

Associativity

Description

Example

20

( )

N/A

Expression grouping (brackets)

(1+2)

19

.

Left-to-right

Member of (object)

country.state

[]

Left-to-right

Member of (array)

country["state"]

()

Left-to-right

Function call

testFunction()

new (with argument list)

N/A

Create statement / operator

new Date()

18

new (without argument list)

Right-to-left

Create statement / operator

new

17

++

N/A

Postfix increment

x++

--

N/A

Postfix decrement

x--

16

++

Right-to-left

Prefix increment

++y

--

Right-to-left

Prefix decrement

--y

+ (unary)

Right-to-left

Unary plus

+y

- (unary)

Right-to-left

Unary minus

-y

!

Right-to-left

Logical not

!(x==20)

~

Right-to-left

Bitwise not

~5

typeof

Right-to-left

typeof operator

typeof x

void

Right-to-left

void operator

void()

delete

Right-to-left

Delete operator

delete x.value

15

**

Right-to-left

Exponentiation (older browsers do not necessarily support this operator)

10 ** 2

14

*

Left-to-right

Multiplication

4 * 3

/

Left-to-right

Division

6 / 3

%

Left-to-right

Modulo division

7 / 5

13

+

Left-to-right

Addition (or concatenation)

4 + 3

-

Left-to-right

Subtraction

4 – 8

12

<< 

Left-to-right

Bitwise shift left

10 << 2

>> 

Left-to-right

Bitwise shift right

10 >> 2

>>> 

Left-to-right

Bitwise unsigned shift right

-10 >> 2

11

Left-to-right

Less than

x < y

<=

Left-to-right

Less than or equal

x <= y

Left-to-right

Greater than

x > y

>=

Left-to-right

Greater than or equal

x >= y

in

Left-to-right

in operator

x in y

instanceof

Left-to-right

instanceof operator

x instance of Array

10

==

Left-to-right

Equal

x == y

===

Left-to-right

Strictly equal

x === y

!=

Left-to-right

Unequal

x != y

!==

Left-to-right

Strictly unequal

x !== y

9

&

Left-to-right

Bitwise AND

x & y

8

^

Left-to-right

Bitwise XOR

x ^ y

7

|

Left-to-right

Bitwise OR

x | y

6

&&

Left-to-right

Logical AND

x && y

5

||

Left-to-right

Logical OR

x || y

4

? :

Right-to-left

Conditional

bool ? y : z

3

=

Right-to-left

Assignment

x = y

+=

Right-to-left

Assignment with addition (or concatenation)

x += y

-=

Right-to-left

Assignment with subtraction

x *= y

**=

Right-to-left

Assignment with exponentiation

x **= y

*=

Right-to-left

Assignment with multiplication

x *= y

/=

Right-to-left

Assignment with division

x /= y

%=

Right-to-left

Assignment with modulus

x %= y

<<=

Right-to-left

Assignment with left shift

x <<= y

>>=

Right-to-left

Assignment with right shift

x >>= y

>>>=

Right-to-left

Assignment with unsigned right shift

x >>>= y

&=

Right-to-left

Assignment with bitwise AND

x &= y

^=

Right-to-left

Assignment with bitwise XOR

x ^= y

|=

Right-to-left

Assignment with bitwise XOR

x |= y

2

yield

Right-to-left

 

yield x

yield*

Right-to-left

 

yield*

1

N/A

 

… x

0

,

Left-to-right

 

x, y, z

 

The associativity determines the order in which operators of the same precedence are processed. Left-to-right means that a OP b OP c is processed as (a OP b) OP c. Right-to-left means that a OP b OP c is processed as a OP (b OP c).

 

Assignment operators are right-associative (i.e. right-to-left) so that a = b = 4 will result in both a and b being given the value of 4. This is because the assignment operator returns the value that is assigned, i.e. (b=4) returns 4, which is then assigned to a.

 


NAVIGATION LINKS
Contents | Prev | Next | JavaScript Operators


Desktop view | Switch to Mobile