JavaScript Operators

Operators in JavaScript with examples:

Operator
Type

Operator

Description

Example

Result

Arithmetic+Addition5 + 38
 Subtraction5 – 32
 *Multiplication5 * 315
 /Division6 / 32
 %Remainder (modulus)5 % 21
 **Exponentiation2 ** 38
Assignment=Assignmentx = 5x is 5
 +=Addition assignmentx += 3x = x + 3
 -=Subtraction assignmentx -= 2x = x – 2
 *=Multiplication assignmentx *= 2x = x * 2
 /=Division assignmentx /= 2x = x / 2
 %=Remainder assignmentx %= 2x = x % 2
 **=Exponentiation assignmentx **= 2x = x ** 2
Comparison==Equal to5 == ‘5’true
 ===Strict equal to (value and type)5 === ‘5’false
 !=Not equal to5 != ‘5’false
 !==Strict not equal to5 !== ‘5’true
 >Greater than5 > 3true
 <Less than5 < 3false
 >=Greater than or equal to5 >= 5true
 <=Less than or equal to5 <= 3false
Logical&&Logical ANDtrue && falsefalse
 ||Logical ORtrue || falsetrue
 !Logical NOT!truefalse
Bitwise&Bitwise AND5 & 11 (0101 & 0001)
 |Bitwise OR5 | 15 (0101 | 0001)
 ^Bitwise XOR5 ^ 14 (0101 ^ 0001)
 ~Bitwise NOT~5-6 (bitwise NOT of 0101)
 <<Bitwise left shift5 << 110 (0101 << 1)
 >>Bitwise right shift5 >> 12 (0101 >> 1)
 >>>Bitwise unsigned right shift5 >>> 12
Ternary? :Ternary (conditional) operatortrue ? ‘yes’ : ‘no’‘yes’
TypeTypeofReturns the type of a variabletypeof 42“number”
 instanceofChecks if an object is an instance
of a class
obj instanceof Arraytrue if obj is an array

Operator precedence

Here’s a comprehensive table that classifies JavaScript operators, provides examples, and includes their precedence and associativity:

Precedence

Operator

Description

Example

Result

Associativity

1()Grouping(1 + 2) * 39n/a
2new (with argument list)Object Creationnew Date()Creates a new Date
object
n/a
3.Member Accessobj.propertyAccesses property  of objLeft-to-right
 []Computed Member Accessarr[0]Accesses first elementLeft-to-right
4new (without argument list)Object Creationnew ObjectCreates a new ObjectRight-to-left
5++Postfix Incrementx++x, then x + 1n/a
 --Postfix Decrementx--x, then x - 1n/a
6++Prefix Increment++xx + 1, then xRight-to-left
 --Prefix Decrement--xx - 1, then xRight-to-left
 +Unary Plus+xConverts x
to a number
Right-to-left
 -Unary Negation-xNegates xRight-to-left
 ~Bitwise NOT~xInverts bits of xRight-to-left
 !Logical NOT!xInverts x‘s
truthiness
Right-to-left
 typeofType oftypeof xReturns type of xRight-to-left
 voidDiscards return valuevoid xEvaluates x
without returning
Right-to-left
 deleteDeletes propertydelete obj.propertyDeletes property
of obj
Right-to-left
7**Exponentiation2 ** 38Right-to-left
8*Multiplication2 * 36Left-to-right
 /Division6 / 32Left-to-right
 %Remainder5 % 21Left-to-right
9+Addition1 + 23Left-to-right
 -Subtraction5 - 23Left-to-right
10<<Bitwise Left Shift5 << 110Left-to-right
 >>Bitwise Right Shift5 >> 12Left-to-right
 >>>Bitwise Unsigned Right Shift5 >>> 12Left-to-right
11<Less Than5 < 3falseLeft-to-right
 <=Less Than or Equal To5 <= 3falseLeft-to-right
 >Greater Than5 > 3trueLeft-to-right
 >=Greater Than or Equal To5 >= 3trueLeft-to-right
 inProperty in Object'x' in objtrue if obj
has x
Left-to-right
 instanceofInstance of Objectobj instanceof Arraytrue if obj
is an Array
Left-to-right
12==Equal5 == '5'trueLeft-to-right
 !=Not Equal5 != '5'falseLeft-to-right
 ===Strict Equal5 === '5'falseLeft-to-right
 !==Strict Not Equal5 !== '5'trueLeft-to-right
13&Bitwise AND5 & 11Left-to-right
14^Bitwise XOR5 ^ 14Left-to-right
15``Bitwise OR`51`
16&&Logical ANDtrue && falsefalseLeft-to-right
17` `Logical OR`true
18??Nullish Coalescingnull ?? 'default''default'Left-to-right
19? :Conditional (Ternary)true ? 'yes' : 'no''yes'Right-to-left
20=Assignmentx = 5x is 5Right-to-left
 +=Addition Assignmentx += 3x = x + 3Right-to-left
 -=Subtraction Assignmentx -= 2x = x - 2Right-to-left
 *=Multiplication Assignmentx *= 2x = x * 2Right-to-left
 /=Division Assignmentx /= 2x = x / 2Right-to-left
 %=Remainder Assignmentx %= 2x = x % 2Right-to-left
 **=Exponentiation Assignmentx **= 2x = x ** 2Right-to-left
 <<=Left Shift Assignmentx <<= 1x = x << 1Right-to-left
 >>=Right Shift Assignmentx >>= 1x = x >> 1Right-to-left
 >>>=Unsigned Right Shift Assignmentx >>>= 1x = x >>> 1Right-to-left
 &=Bitwise AND Assignmentx &= 1x = x & 1Right-to-left
 ^=Bitwise XOR Assignmentx ^= 1x = x ^ 1Right-to-left
 `=`Bitwise OR Assignment`x= 1`
21,Commax = 1, y = 2y is 2Left-to-right
  • Associativity defines the order in which operators of the same precedence are processed.
    Left-to-right means they are processed from left to right, and right-to-left means they are processed from right to left.

Let’s illustrate operator precedence with an example that combines several types of operators.

Example:

let result = 5 + 3 * 2 ** 2 - (6 / 3) && 4 || false;

Let’s break down this
expression step by step according to operator precedence:

1.    Grouping (()):

    • Evaluate (6 / 3) first because of parentheses.
    • Result: 2

2.    Exponentiation (**):

    • Evaluate 2 ** 2 next.
    • Result: 4

3.    Multiplication (*):

    • Evaluate 3 * 4 next.
    • Result: 12

4.    Addition and Subtraction (+-):

    • Evaluate 5 + 12 next.
    • Result: 17
    • Then evaluate 17 - 2.
    • Result: 15

5.    Logical AND (&&):

    • Evaluate 15 && 4.
    • Result: 4 (since 15
      is truthy, the result is 4)

6.    Logical OR (||):

    • Evaluate 4 || false.
    • Result: 4 (since 4
      is truthy, the result is 4)

So, the final result of the expression let result = 5 + 3 * 2 ** 2 - (6 / 3) && 4 || false; is 4.

Scroll to Top