JavaScript tutorial:
Operator precedence

 

Operators in JavaScript are evaluated in a particular order. This order is known as the operator precedence. The following table lists the operators in highest to lowest precedence order. Operators with the same precedence are evaluated in left to right order in the expression.

Operator

Description

. [] ()

Field access, array indexing, and function calls

++ -- - ~ ! delete new typeof void

Unary operators, return data type, object creation, undefined values

* / %

Multiplication, division, modulo division

+ - +

Addition, subtraction, string concatenation

<< >> >>>

Bit shifting

< <= > >= instanceof

Less than, less than or equal, greater than, greater than or equal, instanceof

== != === !==

Equality, inequality, identity, nonidentity

&

Bitwise AND

^

Bitwise XOR

|

Bitwise OR

&&

Logical AND

||

Logical OR

?:

Conditional

= OP=

Assignment, assignment with operation

,

Multiple evaluation

Parentheses are used to alter the order of evaluation. The expression within parentheses is fully evaluated before its value is used in the remainder of the statement.

An operator with higher precedence is evaluated before one with lower precedence. For example:

z = 78 * (96 + 3 + 45)

There are five operators in this expression: =, *, (), +, and +. According to precedence, they are evaluated in the following order: (), *, +, +, =.

  1. Evaluation of the expression within the parentheses is first: There are two addition operators, and they have the same precedence: 96 and 3 are added together and 45 is added to that total, resulting in a value of 144.

  2. Multiplication is next: 78 and 144 are multiplied, resulting in a value of 11232.

  3. Assignment is last: 11232 is assigned into z.