Operator Precedence in JavaScript

Operator precedence refers to the priority given to operators when parsing a statement in which multiple operators perform operations. It is important to ensure correct results and to allow the compiler to understand the order of operations. Operators with higher precedence are resolved first. However, the further down the list you go, the lower the priority and the lower the resolution.

Precedence and Associativity: Associativity generally indicates that the result remains the same regardless of the order of the operands of a particular operation. Priorities are used to tell the compiler which operations should be performed first. For example, consider the three numbers 2, 3, and 4.

Now consider two operations:

( 2 + 3 ) + 4 = 2 + ( 3 + 4 )
( 2 >= 3 ) or ( 1 != 4 )

The first operation is associative, the order is not important. The second case is priority. In this case, the operations must be performed in the correct order to achieve the desired result.

Associativity is not a single concept, but processing of precedence operations must be done with either left-to-right or right-to-left associativity. This depends entirely on the operation and tells the parser in which direction to start the operation.

Example: 

// left-to-right associativity : division 
3/4
// right-to-left associativity : assignment 
a = 3 

Operator Precedence Table: The operator priority table helps determine the priority of an operator compared to other operators. These operators have relatively lower precedence as you move down the table. That is, an operator has a lower precedence than the operator above it and a higher precedence than the operator below it. Operators on the same line have the same priority.

In this table, 1 is the highest precedence and 19 is the lowest precedence.

Precedence  Operator Description Associativity Example
1 () Grouping (1+2)
2 . Member left to right obj.function
[] Member left to right obj[“func”]
new Create new Date()
() Function call left to right func()
3 ++ Postfix increment i++
Postfix decrement i–
4 ++ Prefix increment right to left ++i
Prefix decrement –i
! Logical NOT !TRUE
typeof Type typeof a
5 ** Exponentiation right to left 4**2
6 * Multiplication left to right 2*3
/ Division 18/9
% Remainder 4%2
7 + Addition left to right 2+4
Subtraction 4-2
8 << Left shift left to right y<<2
>> Right shift y>>2
>>> Unsigned Right shift y>>>2
9 < Less than left to right 3<4
<= Less than or equal 3<=4
> Greater than 4>3
>= Greater than or equal 4>=3
in In “PI” in MATH
instanceof Instance of A instanceof B
10 == Equality left to right x==y
!= Inequality x!=y
=== Strictly equal x===y
!== Strictly unequal x!==y
11 & Bitwise AND left to right x&y
12 ^ Bitwise XOR left to right x^y
13 | Bitwise OR left to right x|y
14 && Logical AND left to right x&&y
15 || Logical OR left to right x||y
16 ? : Conditional right to left (x>y)?x:y
17 Assignment right to left x=5
+= x+=3
-= x-=3
*= x*=3
/= x/=3
%= x%=3
<<= x<<=2
>>= x>>=2
>>>= x>>>=2
&= x&=y
^= x^=y
|= x|=y
18 yield Pause function right to left yield x
19 , Comma left to right x,y
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, codeConfig.in Courses are your key to success. We provide top-quality content at no cost, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!