# JavaScript - Operators

Operators are used to perform operation on a single operand or two operands. Operators in JavaScript can be categorized as follows:

- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- Bitwise operators
- Miscellaneous operators

## JavaScript Arithmetic operators

Arithmetic operators are used to perform arithmetic operations on two operands.

Operator | Name | Description |
---|---|---|

+ | Addition | Add two values |

- | Subtraction | Subtract one value from another |

* | Multiplication | Multiply two values |

/ | Division | Divide one value by another |

** | Exponent / Power | Returns first operand raised to the power of second operand |

% | Modulo | Returns remainder of division operation |

Example

## JavaScript Assignment operators

Assignment operators are used to assign values of right hand side expression to left hand side operand.

Operator | Expression | Equivalent to | Example |
---|---|---|---|

= | a = 5 | a = 5 | Example |

+= | a += b | a = a + b | |

-= | a -= b | a = a - b | |

*= | a *= b | a = a * b | |

/= | a /= b | a = a / b | |

**= | a **= b | a = a ** b | |

%= | a %= b | a = a % b | |

&&= | a &&= b | a = a && b | |

||= | a ||= b | a = a || b | |

??= | a ??= b | a = a ?? b | |

&= | a &= b | a = a & b | More Info |

|= | a |= b | a = a | b | More Info |

^= | a ^= b | a = a ^ b | More Info |

>>= | a >>= b | a = a >> b | More Info |

>>>= | a >>>= b | a = a >>> b | |

<<= | a <<= b | a = a << b | More Info |

**Note:** >>>= is just like the >>= operator, except that the bits shifted in on the left are always zero.

## JavaScript Comparison operators

Comparison operators are used to compare values of two operands. It returns true when values matches and returns false when values does not match.

Operator | Description |
---|---|

== | Equal |

=== | Equal value and equal type |

!= | Not equal |

!== | Not equal value or not equal type |

> | Greater than |

< | Less than |

>= | Greater than or equal to |

<= | Less than or equal to |

Example

## JavaScript Increment/Decrement operators

Increment and decrement operators are used to increase and decrease the value of variable.

Operator | Name | Description | Example |
---|---|---|---|

++x | Pre-increment | Increases the value of x by 1, then returns x. | Example |

x++ | Post-increment | Returns x, then increases the value of x by 1. | |

--x | Pre-decrement | Decreases the value of x by 1, then returns x. | Example |

x-- | Post-decrement | Returns x, then decreases the value of x by 1. |

## JavaScript Logical operators

Logical operators are used to combine two or more conditions.

Operator | Name | Description |
---|---|---|

and | Logical AND | Returns true when all conditions are true |

&& | ||

or | Logical OR | Returns true when any of the conditions is true |

|| | ||

not | Logical NOT | Returns true when given conditions is not true |

! |

More Info

## JavaScript Bitwise operators

Bitwise operators are used to perform bitwise operations on two operands.

Operator | Name | Description | More Info |
---|---|---|---|

& | AND | Returns 1 if both bits at the same position in both operands are 1, else returns 0. | More Info |

| | OR | Returns 1 if one of two bits at the same position in both operands is 1, else returns 0. | More Info |

^ | XOR | Returns 1 if only one of two bits at the same position in both operands is 1, else returns 0. | More Info |

~ | NOT | Reverse all the bits. | More Info |

>> | Right shift | The left operand is moved right by the number of bits present in the right operand. | More Info |

>>> | Right shift with Zero | This operator is just like the >> operator, except that the bits shifted in on the left are always zero. | |

<< | Left shift | The left operand value is moved left by the number of bits present in the right operand. | More Info |

## JavaScript Miscellaneous operators

The below table describes other operators supported by JavaScript:

Operator | Description |
---|---|

comma operator (,) | Evaluates each of its operands (from left to right) and returns the value of the last operand. |

delete operator | Deletes the specified property of an object. |

in operator | Checks if the specified property is in the specified object. |

instanceof operator | Returns true if the object is an instance of a specific class, false otherwise. |

Nullish coalescing operator (??) | Returns right-hand side operand when left-hand side operand is null or undefined, otherwise returns left-hand side operand. |

ternary operator (?:) | Returns one of the two values based on value of boolean expression. |

typeof operator | Returns the type of a JavaScript variable or expression. |

## JavaScript Operators Precedence

Operator precedence (**order of operations**) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given expression.

For example, multiplication has higher precedence than addition. Thus, the expression 1 + 2 × 3 is interpreted to have the value 1 + (2 × 3) = 7, and not (1 + 2) × 3 = 9. When exponent is used in the expression, it has precedence over both addition and multiplication. Thus 3 + 5^{2} = 28 and 3 × 5^{2} = 75.

The following table lists the precedence and associativity of JavaScript operators. Operators are listed top to bottom, in **descending precedence**. Operators with higher precedence are evaluated before operators with relatively lower precedence. When operators have the same precedence, associativity of the operators determines the order in which the operations are performed.

Precedence | Operator | Description | Associativity |
---|---|---|---|

21 | ( ) | Grouping | NA |

20 | new | new (with argument list) | |

. | Member Access | Left to Right | |

[ ] | Computed Member Access | ||

( ) | Function Call | ||

?. | Optional chaining | ||

19 | new | new (without argument list) | Right to Left |

18 | a++ a-- | Postfix Increment, Postfix Decrement | NA |

17 | ! | Logical NOT | Right to Left |

~ | Bitwise NOT | ||

+a -a | Unary plus, Unary negation | ||

++a --a | Prefix Increment, Prefix Decrement | ||

typeof | Type | ||

void | void operator | ||

delete | delete operator | ||

await | await operator | ||

16 | ** | Exponentiation | |

15 | * / % | Multiplication, Division, Remainder | Left to Right |

14 | + - | Addition, Subtraction | |

13 | << >> >>> | Bitwise left shift, right shift and unsigned right shift | |

12 | < <= > >= | Less than, Less than or equal, Greater than, and Greater than or equal | |

in | Property in Object | ||

instanceof | Instance of Object | ||

11 | == != | Equality, Inequality | |

=== !== | Strict Equality, Strict Inequality | ||

10 | & | Bitwise AND | |

9 | ^ | Bitwise XOR | |

8 | | | Bitwise OR | |

7 | && | Logical AND | |

6 | || | Logical OR | |

5 | ?? | Nullish coalescing operator | |

4 | ?: | Conditional (ternary) operator | Right to Left |

3 | = | Direct assignment | |

+= -= **= *= /= %= | Compound assignment by sum, difference, exponentiation, product, quotient and remainder | ||

<<= >>= >>>= | Compound assignment by Bitwise left shift, right shift and unsigned right shift | ||

&= ^= |= | Compound assignment by Bitwise AND, XOR and OR | ||

&&= ||= | Compound assignment by Logical AND, OR | ||

??= | Compound assignment by nullish coalescing operator | ||

2 | yield yield* | Pause Function | |

1 | , | Comma / Sequence | Left to Right |