Javascript
JS Operators
Operator Precedence

Operator Precedence in JavaScript

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 be solved first while going from left to right if two or more operators have the same precedence in the expression.

Example:

2 + 3 * 4 / 3 // 6

The precedence of the multiply (*) and divide (/) operators is the same, but due to left-to-right associativity, the multiply operator will be resolved first, resulting in 6.

Operator Precedence and Associativity Table

The operator precedence and associativity table can help determine the precedence of an operator relative to others. As one goes down the table, the precedence of these operators decreases. Operators listed under the same precedence level share the same priority and associativity.

PrecedenceOperatorDescriptionAssociativityExample
1()Grouping–(1+2)
2.MemberLeft to Rightobj.function
2.1[]MemberLeft to Rightbrand["carName"]
2.2newCreate–new Date("July 27, 2023")
2.3()Function Call–myFun()
3++Postfix incrementN/Ai++
3.1--Postfix decrementN/Ai--
4++Prefix incrementRight to Left++i
4.1--Prefix 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/Division–18 / 9
6.2%Remainder–4 % 2
7+AdditionLeft to Right2 + 4
7.1-Subtraction–4 - 2
8<<Left shiftLeft to Righty << 2
8.1>>Right shift–y >> 2
8.2>>>Unsigned right shift–y >>> 2
9<Less thanLeft to Right3 < 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.4inIn–'PI' in Math
9.5instanceofInstance of–A instanceof B
10==EqualityLeft to Rightx == y
10.1!=Inequality–x != y
10.2===Strictly equal–x === y
10.3!==Strictly unequal–x !== y
11&Bitwise ANDLeft to Rightx & y
12^Bitwise XORLeft to Rightx ^ y
13``Bitwise ORLeft to Right
14&&Logical ANDLeft to Rightx && y
15``Logical OR
16? :ConditionalRight to Left(x > y) ? x : y
17=AssignmentRight to Leftx = 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–
18yieldPause functionRight to Leftyield x
19,CommaLeft to Rightx, y

This table provides a structured view of how JavaScript resolves operators in expressions.