JavaScript Operators


Operators in JavaScript with examples:

Operator
Type

Operator

Description

Example

Result

Arithmetic

+

Addition

5 + 3

8

 

Subtraction

5 – 3

2

 

*

Multiplication

5 * 3

15

 

/

Division

6 / 3

2

 

%

Remainder (modulus)

5 % 2

1

 

**

Exponentiation

2 ** 3

8

Assignment

=

Assignment

x = 5

x is 5

 

+=

Addition assignment

x += 3

x = x + 3

 

-=

Subtraction assignment

x -= 2

x = x – 2

 

*=

Multiplication assignment

x *= 2

x = x * 2

 

/=

Division assignment

x /= 2

x = x / 2

 

%=

Remainder assignment

x %= 2

x = x % 2

 

**=

Exponentiation assignment

x **= 2

x = x ** 2

Comparison

==

Equal to

5 == ‘5’

true

 

===

Strict equal to (value and type)

5 === ‘5’

false

 

!=

Not equal to

5 != ‘5’

false

 

!==

Strict not equal to

5 !== ‘5’

true

 

Greater than

5 > 3

true

 

Less than

5 < 3

false

 

>=

Greater than or equal to

5 >= 5

true

 

<=

Less than or equal to

5 <= 3

false

Logical

&&

Logical AND

true && false

false

 

||

Logical OR

true || false

true

 

!

Logical NOT

!true

false

Bitwise

&

Bitwise AND

5 & 1

1 (0101 & 0001)

 

|

Bitwise OR

5 | 1

5 (0101 | 0001)

 

^

Bitwise XOR

5 ^ 1

4 (0101 ^ 0001)

 

~

Bitwise NOT

~5

-6 (bitwise NOT of 0101)

 

<< 

Bitwise left shift

5 << 1

10 (0101 << 1)

 

>> 

Bitwise right shift

5 >> 1

2 (0101 >> 1)

 

>>> 

Bitwise unsigned right shift

5 >>> 1

2

Ternary

? :

Ternary (conditional) operator

true ? ‘yes’ : ‘no’

‘yes’

Type

Typeof

Returns the type of a variable

typeof 42

“number”

 

instanceof

Checks if an object is an instance
of a class

obj instanceof Array

true 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) * 3

9

n/a

2

new (with argument list)

Object Creation

new Date()

Creates a new Date
object

n/a

3

.

Member Access

obj.property

Accesses property  of obj

Left-to-right

[]

Computed Member Access

arr[0]

Accesses first element

Left-to-right

4

new (without argument list)

Object Creation

new Object

Creates a new Object

Right-to-left

5

++

Postfix Increment

x++

x, then x + 1

n/a

--

Postfix Decrement

x--

x, then x - 1

n/a

6

++

Prefix Increment

++x

x + 1, then x

Right-to-left

--

Prefix Decrement

--x

x - 1, then x

Right-to-left

+

Unary Plus

+x

Converts x
to a number

Right-to-left

-

Unary Negation

-x

Negates x

Right-to-left

~

Bitwise NOT

~x

Inverts bits of x

Right-to-left

!

Logical NOT

!x

Inverts x‘s
truthiness

Right-to-left

typeof

Type of

typeof x

Returns type of x

Right-to-left

void

Discards return value

void x

Evaluates x
without returning

Right-to-left

delete

Deletes property

delete obj.property

Deletes property
of obj

Right-to-left

7

**

Exponentiation

2 ** 3

8

Right-to-left

8

*

Multiplication

2 * 3

6

Left-to-right

/

Division

6 / 3

2

Left-to-right

%

Remainder

5 % 2

1

Left-to-right

9

+

Addition

1 + 2

3

Left-to-right

-

Subtraction

5 - 2

3

Left-to-right

10

<< 

Bitwise Left Shift

5 << 1

10

Left-to-right

>> 

Bitwise Right Shift

5 >> 1

2

Left-to-right

>>> 

Bitwise Unsigned Right Shift

5 >>> 1

2

Left-to-right

11

< 

Less Than

5 < 3

false

Left-to-right

<=

Less Than or Equal To

5 <= 3

false

Left-to-right

> 

Greater Than

5 > 3

true

Left-to-right

>=

Greater Than or Equal To

5 >= 3

true

Left-to-right

in

Property in Object

'x' in obj

true if obj
has x

Left-to-right

instanceof

Instance of Object

obj instanceof Array

true if obj
is an Array

Left-to-right

12

==

Equal

5 == '5'

true

Left-to-right

!=

Not Equal

5 != '5'

false

Left-to-right

===

Strict Equal

5 === '5'

false

Left-to-right

!==

Strict Not Equal

5 !== '5'

true

Left-to-right

13

&

Bitwise AND

5 & 1

1

Left-to-right

14

^

Bitwise XOR

5 ^ 1

4

Left-to-right

15

`

`

Bitwise OR

`5

1`

16

&&

Logical AND

true && false

false

Left-to-right

17

`

`

Logical OR

`true

18

??

Nullish Coalescing

null ?? 'default'

'default'

Left-to-right

19

? :

Conditional (Ternary)

true ? 'yes' : 'no'

'yes'

Right-to-left

20

=

Assignment

x = 5

x is 5

Right-to-left

+=

Addition Assignment

x += 3

x = x + 3

Right-to-left

-=

Subtraction Assignment

x -= 2

x = x - 2

Right-to-left

*=

Multiplication Assignment

x *= 2

x = x * 2

Right-to-left

/=

Division Assignment

x /= 2

x = x / 2

Right-to-left

%=

Remainder Assignment

x %= 2

x = x % 2

Right-to-left

**=

Exponentiation Assignment

x **= 2

x = x ** 2

Right-to-left

<<=

Left Shift Assignment

x <<= 1

x = x << 1

Right-to-left

>>=

Right Shift Assignment

x >>= 1

x = x >> 1

Right-to-left

>>>=

Unsigned Right Shift Assignment

x >>>= 1

x = x >>> 1

Right-to-left

&=

Bitwise AND Assignment

x &= 1

x = x & 1

Right-to-left

^=

Bitwise XOR Assignment

x ^= 1

x = x ^ 1

Right-to-left

`

=`

Bitwise OR Assignment

`x

= 1`

21

,

Comma

x = 1, y = 2

y is 2

Left-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