Javascript Tutorial
Javascript Tutorial Introduction to Javascript JavaScript Code Editors & IDE JavaScript Hello World Example Javascript Syntax and Rules syntax_rules javascript_identifiers JavaScript Keywords & Reserved Words javascript_variables JavaScript Const JavaScript let vs var vs const Data Types in JavaScript JavaScript String Template Literals & String interpolation in JavaScript Tagged Templates in JavaScript String to Number in JavaScript Number Data Type in JavaScript NaN in JavaScript JavaScript Number Min & Max & Safe Values JavaScript EPSILON & Floating point precision Infinity in JavaScript JavaScript Bigint BigInt Vs Number in JavaScript Boolean Data Type in JavaScript Undefined in JavaScript Null in JavaScript Null vs Undefined in JavaScript JavaScript Operators Arithmetic Operators in JavaScript Unary plus & minus operators in JavaScript Increment & Decrement Operators in JavaScript Comparison or Relational operators in JavaScript Strict Equality (==) Loose Equality (===) in JavaScript Ternary Conditional Operator in JavaScript Logical Operators in JavaScript Bitwise Operators in JavaScript Assignment Operators in JavaScript Nullish Coalescing Operator in JavaScript Comma Operator in JavaScript Typeof JavaScript Operator Precedence in JavaScript JavaScript if, else & nested if statement Switch Statement in JavaScript While & Do While Loops in JavaScript For Loop in JavaScript Break statement in JavaScript Continue Statement in JavaScript Arrays in JavaScript Array Constructor in Javascript Sparse Array Vs Dense Array in JavaScript How to merge Arrays in JavaScript Array Methods in JavaScript Functions in JavaScript Function Parameters & Arguments in JavaScript JavaScript Default Parameters Pass by Value and Pass by Reference in Javascript Function Expression in Javascript Nested Functions in JavaScript Immediately-invoked Function Expressions (IIFE) JavaScript Callback Functions Arrow Functions in JavaScript Arguments Object In JavaScript Rest Parameters in JavaScript Objects in Javascript Create Objects in JavaScript JavaScript Object Properties Computed Property Names in JavaScript Object Literal in JavaScript Constructor Function & New Operator in JavaScript Delete Operator in JavaScript hasOwnProperty in JavaScript Using Getters and Setters in Javascript DefineProperty in JavaScript JavaScript Property Descriptors Enumerable, Writable & Configurable Object Destructuring in JavaScript Variable Scope in JavaScript Hoisting in JavaScript Lexical Scope & Closures in JavaScript This in JavaScript Global Object, Window & Globalthis in JavaScript Call function in Javascript Prototype In Javascript Prototype Inheritance in JavaScript Instanceof Operator in JavaScript Spread Operator in JavaScript

Operator Precedence in JavaScript

The operator precedence along with their associativity determines how JavaScript evaluates an expression when there are multiple JavaScript Operators present in the expression. Each JavaScript operators have certain precedence. The JavaScript evaluates the operators with higher precedence first. If a group of operators has the same Precedence, then it evaluates them either left to right or right to left, depending on the operator’s associativity. The table end of the article. has a list of all operators, with their precedence along with associativity.

Operator Precedence

For Example, take a simple arithmetic operation.

                            
100+100*20
                     
                            
                        

If we did the math from left to right and without any precedence we would add 100+100=200, and then multiply it by 20. The answer would be 4000. But that is not what we get.

Multiplication always has higher precedence than an addition. Hence it performs the multiplication first (100*20=2000) and then the addition (100+2000=2100)

                            
 console.log((100+100)*20);   //4000
                     
                            
                        

Operators Associativity

The associativity defines the direction in which the evaluation takes place. It can be either left to right (left-associativity) or right to left (right associativity).

  1. Associativity comes into the picture only when the operators have the same precedence.
  2. All operators with the same precedence have the same associativity

The assignment Operator = has a right to left associativity.

  1. y=10 is evaluated first. y becomes 10
  2. x=y is next. Hence x becomes 10.
                            
y=5
x = y = 10
 
                     
                            
                        

TNote that in the above example if = had left to right associativity, then the value of x will be 5.

Multiplication, division & reminder has the same Precedence and left-to-right associativity. The expression 10/5*20/8 is evaluated in the following order.

  1. 10/5 = 2
  2. 2*20 = 40
  3. 40/8 = 5

Parenthesis (grouping) and short-circuiting

The Parenthesis or grouping operator has the highest precedence. However, even they might not be evaluated, when they are part of the expression involving short-circuited Operators.

The Logical operators like || & && stop the evaluation, if they find the match This is known as a short-circuiting. For Example, the code below does not invoke alert("Hello") even with a parenthesis.

Similarly in expression, a || (b * c) b*c never invoked if a is truthy.

                            
console.log(true || (alert("Hello")))
 
//alert is never evaluated
 
 
 
let a =10
let b = 5
let c = 5
 
console.log(a || (b * c));   //10    b*c never invoked
 
 
                     
                            
                        

The logical AND, nullish-coalescing, optional chaining, and conditional operator are the other short-circuited operators.

Operator Precedence & Associativity Table

Precedence Operator type Associativity Individual operators
21 Grouping n/a ( … )
20 Member Access left-to-right … . …
Computed Member Access left-to-right … [ … ]
new (with argument list) n/a new … ( … )
Function Call left-to-right … ( … )
Optional chaining left-to-right ?.
19 new (without argument list) right-to-left new …
18 Postfix Increment n/a … ++
Postfix Decrement … --
17 Logical NOT right-to-left ! …
Bitwise NOT ~ …
Unary Plus + …
Unary Negation - …
Prefix Increment ++ …
Prefix Decrement -- …
typeof typeof …
void void …
delete delete …
await await …
16 Exponentiation right-to-left … ** …
15 Multiplication left-to-right … * …
Division … / …
Remainder … % …
14 Addition left-to-right … + …
Subtraction … - …
13 Bitwise Left Shift left-to-right … << …
Bitwise Right Shift … >> …
Bitwise Unsigned Right Shift … >>> …
12 Less Than left-to-right … < …
Less Than Or Equal … <= …
Greater Than … > …
Greater Than Or Equal … >= …
in … in …
instanceof … instanceof …
11 Equality left-to-right … == …
Inequality … != …
Strict Equality … === …
Strict Inequality … !== …
10 Bitwise AND left-to-right … & …
9 Bitwise XOR left-to-right … ^ …
8 Bitwise OR left-to-right … | …
7 Logical AND left-to-right … && …
6 Logical OR left-to-right … || …
5 Nullish coalescing operator left-to-right … ?? …
4 Conditional left-to-right … ? … : …
3 Assignment right-to-left … = …
… += …
… -= …
… **= …
… *= …
… /= …
… %= …
… <<= …
… >>= …
… >>>= …
… &= …
… ^= …
… |= …
… &&= …
… ||= …
… ??= …
2 yield right-to-left yield …
yield* yield* …
1 Comma / Sequence left-to-right … , …