Open In App

Operator precedence in JavaScript

Last Updated : 28 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Operator precedence refers to the priority given to operators while parsing a statement that has more than one operator performing operations in it. Operators with higher priorities are resolved first. But as one goes down the list, the priority decreases and hence their resolution.

( * ) and ( / ) have higher precedence than ( + ) and ( - )

Precedence and Associativity

The associativity represents which operator has to solve first while going from left to right if two or more operators have the same precedence in the expression.

Example:

2 + 3 * 4 / 3 =  2 + (3 * 4) / 3  // 6

The precedence of the multiply(*) and divide(/) operators is the same but due to associativity from left to right the multiply operator will be resolved first as it comes first when we go from left to right and hence the result will be 6.

Operator Precedence and Associativity Table

The operator precedence and associativity table can help one know the precedence of an operator relative to other operators. As one goes down the table, the precedence of these operators decreases over each other. The operators as subparts of precedence will have the same specified precedence and associativity as contained by the main part.

Precedence  Operator Description Associativity Example
1 () Grouping (1+2)
2 . Member left to right obj.function
2.1 [] Member left to right brand[“carName”]
2.2 new Create new Date(“July 27, 2023”)
2.3 () Function Call myFun()
3 ++ Postfix increment N/A i++
3.1 Postfix Decrement N/A i–
4 ++ Prefix increment right to left ++i
4.1 Prefix Decrement N/A –i
4.2 ! Logical NOT N/A !(x===y)
4.3 typeof Type N/A typeof a
5 ** Exponentiation right to left 4**2
6 * Multiplication left to right 2*3
6.1 / Division 18/9
6.2 % Remainder 4%2
7 + Addition left to right 2+4
7.1 Subtraction 4-2
8 <<  Left shift  left to right y<<2
8.1  >> Right Shift y>>2
8.2  >>>  Unsigned right shift y>>>2
9 Less than left to right 3<4
9.1  <= Less than or equal to 3<=4
9.2  > Greater than 4>3
9.3 >= Greater than or equal to 4>=3
9.4 In In “PI” in MATH
9.5 Instance of  Instance of A instanceof B
10 == Equality left to right x==y
10.1 != Inequality x!=y
10.2 === Strictly equal x===y
10.3 !== 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
17.1 += Addition assignment x+=5 // x=x+5
17.2 -= Subtraction assignment x-=5 // x=x-5
17.3 *= Multiplication assignment x*=5 // x = x*5
17.4 /= Division assignment x/=5 // x = x/5
17.5 %= Modulo assignment x%=5 // x = x%5
17.6 <<= left shift assignment x<<=5 // x=x<<5
17.7 >>= right shift assignment x>>=5 // x=x>>5
17.8 >>>= Unsigned right shift assignment x>>>=5 // x=x>>>5
17.9 &= Bitwise AND assignment x&=5 // x=x&5
17.10 ^= Bitwise XOR assignment x^=5 // x=x^5
17.11 |= Bitwise OR assignment x|=5 // x=x|5
18 yield Pause function  right to left yield x
19 , Comma left to right x,y


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads