More on Operators

The JavaScript execution sequence is one statement at-a-time, left-to-right (like we would read English). However operators and certain programming constructs(if/else, for, while etc.) can alter this. We now look at how operators affect the execution sequence.

Operator Precedence

Consider the assignment operator:

var x = 2 + 3;

The assignment operator has 2 operands, commonly known as the left-hand-side (var x) and the right-hand-side (2 + 3). The default execution sequence dicatates that the left-hand-side be evaluated by the interpreter first. The assignment operator changes this. It dictates that the right-hand-side is evaluated first. Thus the expression 2 + 3 is evaluated (5) and then is assigned to the left-hand-side. That is, the variable declaration and assignment happens after the right-hand-side expression is evaluated.

All other operators also alter the execution sequence. The interpreter uses the precedence order to evaluate operators. The following table lists the precedence of each JS operator.

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 right-to-left … ? … : …
3 Assignment right-to-left … = …
… += …
… -= …
… **= …
… *= …
… /= …
… %= …
… <<= …
… >>= …
… >>>= …
… &= …
… ^= …
… |= …
… &&= …
… ||= …
… ??= …
2 yield right-to-left yield …
yield* yield* …
1 Comma / Sequence left-to-right … , …

Operator Polymorphism

The same operator can perform different operations depending on the operands provided to it. This is known as operator polymorphism.

This concept is best explained using the addition(+) operator.

Look at the following examples:

var num = 10;
var st = "hello";

typeof num; // "number"
typeof st; // "string"

var x = num + 20; // 30
typeof x; // "number"

var y = st + "world"; // "hello world"
typeof y; // "string"

var z = num + st; "10hello"
typeof z; // "string"

z = st + num; "hello10"
typeof z; // "string"

Comparison Operators

These are binary operators that compare if the operands are equal to, greater than, less than etc.

Operator Description Examples returning true
Equal (==) Returns true if the operands are equal. 3 == var1

"3" == var1

3 == '3'
Not equal (!=) Returns true if the operands are not equal. var1 != 4
var2 != "3"
Strict equal (===) Returns true if the operands are equal and of the same type. 3 === var1
Strict not equal (!==) Returns true if the operands are of the same type but not equal, or are of different type. var1 !== "3"
3 !== '3'
Greater than (>) Returns true if the left operand is greater than the right operand. var2 > var1
"12" > 2
Greater than or equal (>=) Returns true if the left operand is greater than or equal to the right operand. var2 >= var1
var1 >= 3
Less than (<) Returns true if the left operand is less than the right operand. var1 < var2
"2" < 12
Less than or equal (<=) Returns true if the left operand is less than or equal to the right operand. var1 <= var2
var2 <= 5

Logical Operators

These operators can be used to combine 2 comparison operators. The operands are Boolean (or else they are typecast to Boolean).

Operator Description Example var x = 9, y = 2;
&& and (x < 10 && y > 1) is true
|| or (x == 5 || y == 5) is false
! not !(x == y) is true