# Operator Precedence and associativity in JavaScript

## Operator Precedence

Operator precedence determines the order in which operators are evaluated when more than one operator is used in an expression.

The higher an operator’s precedence, the earlier it is evaluated in comparison with the operators with lower precedence.

Consider the following example:

The above expression is evaluated in the order of the multiplication operator (*) at first, then the plus operator (+), and finally, the assignment operator (=), due to their respective precedence order.

Precedence can also be manually overridden using a parenthesis e.g.,

The above mentioned expression has its parenthesis around the plus operator, which alters the precedence thereby evaluating the addition operation first.

## Operator Associativity

Associativity determines the way in which the operators having same precedence are parsed e.g.:

Left-associativity (left to right) denotes that it is processed as (a <operator> b) <operator> c, while right-associativity (right to left) denotes it is interpreted as a <operator> (b <operator>c).

Assignment operators are right-associative i.e.,

Here both a and b get the same value 5.

At first, b is set to 5.

Then a is also initialized to 5 i.e., the return value of b = 5(right operand of the assignment).

## Operator Precedence Table

In the following table, the highest precedence refers to 20 and to the lowest to 1.

Precedence Operator type Associativity Individual operators
20 Grouping n/a (…)
19 Member Access (dot operator) left-to-right … . …
Computed Member Access left-to-right … [ … ]
new (with argument list) n/a new … ( … )
Function Call left-to-right … ( … )
18 new (without argument list) right-to-left new …
17 Postfix Increment n/a … ++
Postfix Decrement … —
16 Logical NOT right-to-left ! …
Bitwise NOT ~ …
Unary Plus + …
Unary Negation – …
Prefix Increment ++ …
Prefix Decrement — …
typeof typeof …
void void …
delete> delete …
await await …
15 Exponentiation right-to-left … ** …
14 Multiplication left-to-right … * …
Division … / …
Remainder … % …
13 Addition left-to-right … + …
Subtraction … – …
12 Bitwise Left Shift left-to-right … << …
Bitwise Right Shift … >> …
Bitwise Unsigned Right Shift … >>> …
11 Less Than left-to-right … < …
Less Than Or Equal … <= …
Greater Than … > …
Greater Than Or Equal … >= …
in … in …
instanceof … instanceof …
10 Equality left-to-right … == …
Inequality … != …
Strict Equality … === …
Strict Inequality … !== …
9 Bitwise AND left-to-right … & …
8 Bitwise XOR left-to-right … ^ …
7 Bitwise OR left-to-right … | …
6 Logical AND left-to-right … && …
5 Logical OR left-to-right … || …
4 Conditional right-to-left … ? … : …
3 Assignment right-to-left … = …
… += …
… -= …
… **= …
… *= …
… /= …
… %= …
… <<= …
… >>= …
… >>>= …
… &= …
… ^= …
… |= …
2 yield right-to-left yield …
yield* yield* …
1 Comma / Sequence left-to-right … , …