In this article, you’ll learn about different operators in JavaScript and how to use them with examples.

JavaScript Operator Types

These are the various operators that JavaScript supports:

Arithmetic operators Assignment operators String operators Comparison operators Logical operators Bitwise operators Special operators

1. Arithmetic Operators

You can use arithmetic operators to perform mathematic calculations on the operands. JavaScript provides the following arithmetic operators:

Addition Operator (+)

You can use the addition operator (+) to perform addition on the operands.

Output:

Subtraction Operator(-)

Use the subtraction operator (-) to subtract the right operand from the left operand.

Output:

Multiplication Operator (*)

Make use of the multiplication operator (*) to multiply the operands.

Output:

Division Operator (/)

You can use the division operator (/) to perform division on the operands.

Output:

Modulus Operator (%)

The modulus operator (%) returns the integer remainder by diving two operands.

Output:

Exponentiation Operator (**)

The exponentiation operator (**) calculates the base to the exponent power (base^exponent).

Output:

Increment Operator (++)

The increment operator (++) increases the integer value by one.

Output:

Decrement Operator (–)

The decrement operator (–) decreases the integer value by one.

Output:

Unary Plus Operator(+)

The unary plus operator (+) tries to convert the operand to a number if it isn’t one already.

Output:

Unary Negation Operator(-)

The unary negation operator (-) returns the negation of its operand.

Output:

2. Assignment Operators

Use assignment operators to assign values to variables. JavaScript provides the following assignment operators:

Assignment Operator(=)

You can use the assignment operator (=) to assign the right operand value to the left operand.

Output:

Addition Assignment Operator(+=)

The addition assignment operator (+=) is used to add the left and right operand values and assign the result to the left operand.

Output:

Subtraction Assignment Operator(-=)

The subtraction assignment operator (-=) will subtract the right operand from the left operand and assign the result to the left operand.

Output:

Multiplication Assignment Operator (*=)

Use the multiplication assignment operator (*=) to multiply the left and right operand values and assign the result to the left operand.

Output:

Division Assignment Operator (/=)

You can use the division assignment operator (/=) to divide the left operand value by the right operand value and assign the result to the left operand.

Output:

Remainder Assignment Operator (%=)

The remainder assignment operator (%=) divides the left operand by the right operand and assigns the remainder to the left operand.

Output:

Exponentiation Assignment Operator (**=)

The exponentiation assignment operator (**=) raises the left operand to the power of the right operand and assigns the result to the left operand.

Output:

The bitwise assignment operators («=, »=, »>=, &=, ^=, |=, &&=, ||=, ??=) work similarly. You’ll learn about the bitwise operators in the upcoming sections.

3. String Operators

The concatenation operator (+) is used to concatenate (add) strings.

Output:

4. Comparison Operators

Comparison operators are used to compare operands and it returns a logical value (true or false) on the basis of comparison. JavaScript provides the following comparison operators:

Equal Operator (==)

The equal operator (==) returns true if the operands are equal. It only compares the values of operands, ignoring their type while comparing.

Output:

Not Equal Operator(!=)

Not equal operator (!=) returns true if the operands are not equal. It also ignores the type of operands while comparing.

Output:

Strict Equal Operator (===)

The strict equal operator (===) returns true if the operands are equal. It compares both—the values and types of operands while comparing.

Output:

Strict Not Equal Operator (!==)

The strict not equal operator (!==) returns true if the operands are not equal. It also compares both—the values and type of operands while comparing.

Output:

Greater Than Operator (>)

The greater than operator (>) returns true if the left operand is greater than the right operand.

Output:

Greater Than or Equal Operator (>=)

The greater than or equal operator (>=) returns true if the left operand is greater than or equal to the right operand.

Output:

Less Than Operator (<)

The less than operator (<) returns true if the left operand is less than the right operand.

Output:

Less Than or Equal Operator(<=)

The less than or equal operator (<=) returns true if the left operand is less than or equal to the right operand.

Output:

5. Logical Operators

According to the official MDN Docs, JavaScript provides the following logical operators:

Logical AND (&&)

Usage: expr1 && expr2

Returns expr1 if it can be converted to false; otherwise, returns expr2. When used with Boolean values, && returns true if both operands are true; otherwise, it returns false.

Output:

Logical OR (||)

Usage: expr1 || expr2

Returns expr1 if it can be converted to true; otherwise, returns expr2. When used with Boolean values, || returns true if either operand is true; if both are false, returns false.

Output:

Logical NOT (!)

Usage: !expr

Returns false if its single operand can be converted to true; otherwise, returns true.

Output:

6. Bitwise Operators

JavaScript provides the following bitwise operators:

Bitwise AND Operator (&)

This operator performs a Boolean AND operation on each bit of its integer arguments.

Output:

Bitwise OR Operator (|)

This operator performs a Boolean OR operation on each bit of its integer arguments.

Output:

Bitwise XOR Operator (^)

This operator performs a Boolean exclusive OR operation on each bit of its integer arguments.

Output:

Bitwise NOT Operator (~)

This operator reverses all the bits in the operand.

Output:

Note: JavaScript converts numbers to 32-bit signed integers before performing a bitwise operation. And when the operation is performed, it converts the result back to 64-bit JavaScript numbers.

Left Shift Operator («)

This operator shifts all the bits in its first operand to the left by the number of places specified in the second operand.

Output:

Right Shift Operator (»)

This operator shifts all the bits in its first operand to the right by the number of places specified in the second operand.

Output:

7. Special Operators

Ternary Operator

The ternary operator is the short-hand of the if-else statement. It assigns value to a variable based on a condition, the syntax for the same is:

If the condition is true, the operator returns the value of value1. Otherwise, it returns the value of value2.

Output:

Typeof Operator

The typeof operator is used to find the data type of a value or variable.

Output:

Use JavaScript One-Liners to Code Like a Pro

You can perform a wide range of operations using just one line of code in JavaScript. Whether you need to shuffle an array, detect dark mode in a browser, generate random UUID, or find the average of an array, you can achieve it with just a line of code.