Open In App

Operator precedence in JavaScript

Last Updated : 28 Dec, 2023
Comments
Improve
Suggest changes
Like Article
Like
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 OperatorDescriptionAssociativityExample
1()Grouping(1+2)
2.Memberleft to rightobj.function
2.1[]Memberleft to rightbrand[“carName”]
2.2newCreatenew Date(“July 27, 2023”)
2.3()Function CallmyFun()
3++Postfix incrementN/Ai++
3.1Postfix DecrementN/Ai–
4++Prefix incrementright to left++i
4.1Prefix DecrementN/A–i
4.2!Logical NOTN/A!(x===y)
4.3typeofTypeN/Atypeof a
5**Exponentiationright to left4**2
6*Multiplicationleft to right2*3
6.1/Division18/9
6.2%Remainder4%2
7+Additionleft to right2+4
7.1Subtraction4-2
8<< Left shift left to righty<<2
8.1 >>Right Shifty>>2
8.2 >>> Unsigned right shifty>>>2
9Less thanleft to right3<4
9.1 <=Less than or equal to3<=4
9.2 >Greater than4>3
9.3>=Greater than or equal to4>=3
9.4InIn“PI” in MATH
9.5Instance of Instance ofA instanceof B
10==Equalityleft to rightx==y
10.1!=Inequalityx!=y
10.2===Strictly equalx===y
10.3!==Strictly unequalx!==y
11&Bitwise ANDleft to rightx&y
12^Bitwise XORleft to rightx^y
13|Bitwise ORleft to rightx|y
14&&Logical ANDleft to rightx&&y
15||Logical ORleft to rightx||y
16? :Conditionalright to left(x>y)?x:y
17 =Assignmentright to leftx=5
17.1+=Addition assignmentx+=5 // x=x+5
17.2-=Subtraction assignmentx-=5 // x=x-5
17.3*=Multiplication assignmentx*=5 // x = x*5
17.4/=Division assignmentx/=5 // x = x/5
17.5%=Modulo assignmentx%=5 // x = x%5
17.6<<=left shift assignmentx<<=5 // x=x<<5
17.7>>=right shift assignmentx>>=5 // x=x>>5
17.8>>>=Unsigned right shift assignmentx>>>=5 // x=x>>>5
17.9&=Bitwise AND assignmentx&=5 // x=x&5
17.10^=Bitwise XOR assignmentx^=5 // x=x^5
17.11|=Bitwise OR assignmentx|=5 // x=x|5
18yieldPause function right to leftyield x
19,Commaleft to rightx,y


Next Article

Similar Reads