An operator is a symbol that tells the interpreter to perform a specific mathematical, relational, or logical operation. Operators take one or more operands and are capable of manipulating target values or operands.

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:

  1. Arithmetic operators
  2. Assignment operators
  3. String operators
  4. Comparison operators
  5. Logical operators
  6. Bitwise operators
  7. 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.

        let a = 12;
let b = 10;
let result = a+b;

console.log(result)
console.log(1+2);
console.log(a+6);

Output:

        22
3
18

Subtraction Operator(-)

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

        let a = 10;
let b = 4;
let result = a-b;

console.log(result);
console.log(23-20);

Output:

        6
3

Multiplication Operator (*)

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

        let a = 10;
let b = 4;
let result = a*b;

console.log(result);
console.log(23*20);

Output:

        40
460

Division Operator (/)

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

        let a = 10;
let b = 4;
let result = a/b;

console.log(result);
console.log(40/20);

Output:

        2.5
2

Modulus Operator (%)

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

        let a = 10;
let b = 4;
let result = a%b;

console.log(result);
console.log(40%20);

Output:

        2
0

Exponentiation Operator (**)

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

        let a = 3;
console.log(a**4);

Output:

        81
    

Increment Operator (++)

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

        let a = 3;
// Value of a=4 and returns 4
console.log(++a);
// Value of a=5 and returns 4
console.log(a++);
// Value of a=5 and returns 5
console.log(a);

Output:

        4
4
5

Decrement Operator (--)

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

        let a = 3;
// Value of a=2 and returns 2
console.log(--a);
// Value of a=1 and returns 2
console.log(a--);
// Value of a=1 and returns 1
console.log(a);

Output:

        2
2
1

Unary Plus Operator(+)

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

        console.log(typeof("10"));
console.log(typeof(+"10"));

console.log(typeof(false));
console.log(typeof(+false));

Output:

        string
number
boolean
number

Unary Negation Operator(-)

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

        let a = 10;
console.log(-a);

Output:

        -10
    

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.

        // Assigning 10 to a
let a = 10;
console.log(a);

Output:

        10
    

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.

        let a = 10;
let b = 5;
// Equivalent to a = a+b
a += b;
console.log(a);

Output:

        15
    

Subtraction Assignment Operator(-=)

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

        let a = 10;
let b = 5;
// Equivalent to a = a-b
a -= b;
console.log(a);

Output:

        5
    

Multiplication Assignment Operator (*=)

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

        let a = 10;
let b = 5;
// Equivalent to a = a*b
a *= b;
console.log(a);

Output:

        50
    

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.

        let a = 10;
let b = 5;
// Equivalent to a = a/b
a /= b;
console.log(a);

Output:

        2
    

Remainder Assignment Operator (%=)

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

        let a = 10;
let b = 5;
// Equivalent to a = a%b
a %= b;
console.log(a);

Output:

        0
    

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.

        let a = 10;
let b = 5;
// Equivalent to a = a**b
a **= b;
console.log(a);

Output:

        100000
    

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

Related: An Introduction to Data Types in JavaScript

3. String Operators

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

        let result = "Make" + "Use" + "Of";
console.log(result);

Output:

        MakeUseOf
    

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.

        console.log(2==4);
console.log("2"==2);

Output:

        false
true

Not Equal Operator(!=)

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

        console.log(2!=4);
console.log(2!="2");

Output:

        true
false

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.

        console.log(2===4);
console.log("2"===2);

Output:

        false
false

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.

        console.log(2 !== 4);
console.log(2 !== "2");

Output:

        true
true

Greater Than Operator (>)

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

        console.log(10>4);
console.log(5>5);

Output:

        true
false

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.

        console.log(10 >= 4);
console.log(5 >= 5);

Output:

        true
true

Less Than Operator (<)

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

        console.log(10<4);
console.log(5<5);

Output:

        false
false

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.

        console.log(10 <= 4);
console.log(5 <= 5);

Output:

        false
true

Related: How to Build a Simple Calculator Using HTML, CSS, and JavaScript

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.

        console.log(true && false);
    

Output:

        false
    

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.

        console.log(true || false);
    

Output:

        true
    

Logical NOT (!)

Usage: !expr

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

        console.log(!true);
    

Output:

        false
    

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.

        // In binary-
// 4: 100
// 1: 001
console.log(4 & 1);

Output:

        0
    

Bitwise OR Operator (|)

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

        console.log(4 | 1);
    

Output:

        5
    

Bitwise XOR Operator (^)

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

        console.log(4 ^ 1);
    

Output:

        5
    

Bitwise NOT Operator (~)

This operator reverses all the bits in the operand.

        console.log(~4);
    

Output:

        -5
    

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.

        console.log(4<<1);
    

Output:

        8
    

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.

        console.log(4>>1);
    

Output:

        2
    

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:

        condition ? value1 : value2
    

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

        let result = (200>100) ? "Yes" : "No";
console.log(result);

Output:

        Yes
    

Typeof Operator

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

        console.log(typeof(100));
console.log(typeof("100"));

Output:

        number
string

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.