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. It is important to ensure the correct result and also to help the compiler understand what the order of operations should be. Operators with higher priorities are resolved first .But as one goes down the list, the priority decreases and hence their resolution.

Precedence and Associativity: Associativity in general states that irrespective of the order of operands for a given operation the result remains the same. Precedence is used to tell the compiler what operations should be performed first. For example, consider 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 associativity where the order does not matter. Second case is precedence, where in order to reach the desired result there has to be a proper order in which operations will be performed. 

Associativity is not a singular concept while dealing with precedence operations has to be dealt either with left-to-right or right-to-left associativity. This completely depends on the operation and tells the parser from which direction the operation should start.

Example:

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

Operator Precedence Table: The operator precedence 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, that is, the priority of an operator is lower than the operators above it and higher than the ones below it. The operators in the same row have the same priority.

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

Precedence OperatorDescriptionAssociativityExample
1()Grouping(1+2)
2.Memberleft to rightobj.function
[]Memberleft to rightobj[“func”]
newCreatenew Date()
()Function callleft to rightfunc()
3++Postfix incrementi++
Postfix decrementi–
4++Prefix incrementright to left++i
Prefix decrement–i
!Logical NOT!TRUE
typeofTypetypeof a
5**Exponentiationright to left4**2
6*Multiplicationleft to right2*3
/Division18/9
%Remainder4%2
7+Additionleft to right2+4
Subtraction4-2
8<< Left shift left to righty<<2
>> Right shifty>>2
>>> Unsigned Right shift y>>>2
9Less thanleft to right3<4
<= Less than or equal3<=4
Greater than4>3
>= Greater than or equal4>=3
inIn“PI” in MATH
instanceofInstance of A instanceof B
10==Equalityleft to rightx==y
!=Inequalityx!=y
===Strictly equalx===y
!==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
+=x+=3
-=x-=3
*=x*=3
/=x/=3
%=x%=3
<<= x<<=2
>>= x>>=2
>>>= x>>>=2
&=x&=y
^=x^=y
|=x|=y
18yieldPause function right to leftyield x
19,Commaleft to rightx,y

Chockalingam