Types of Operators in JavaScript

In this tutorial, we will learn the types of operators in JavaScript. So far, we have learned the basics and now it’s time to learn about operators.

What are Operators in Javascript?

Like other programming languages, an operator in Javascript is a symbol used to perform arithmetic, logical operations on a value or variable, and comparing values. The variables are called operands. The operation to be performed between two operands is defined by an operator.

console.log(a+b);
//Here, a and b are operands, + is an operator.

what are operators in javascript

Types of Operators in Javascript

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison OR Relational operators
  4. Logical operators
  5. Bitwise operators
  6. Ternary operator
  7. typeof operator

Arithmetic Operators in JavaScript

These operators are used to perform arithmetic operations on the operands.
The arithmetic operators are:

Addition (+)

console.log(4+2); // 6

Subtraction (-)

console.log(5-0); // 5

Multiplication (*)

console.log(5*3.5); // 17.5

Division (/)

console.log(3/2); // 1.5

Modulus or Remainder(%)

console.log(5%3); // 2

Exponentiation Or Power (**)

console.log(5**3); //125

Increment (++)

let a=5,b=4;
console.log(a++); // 5
console.log(a); // 6
console.log(++b); // 5
console.log(b); // 5

a++ is called a post-increment operator. It first uses the variable or operand and then increases the value by 1.
++b is called a pre-increment operator. It first increments the value of the variable or operand by 1 and then uses the variable.

Decrement (–)

let a=5,b=4;
console.log(a--); // 5
console.log(a); // 4
console.log(--b); // 3
console.log(b); // 3

a– is called a post-decrement operator. It first uses the variable or operand and then decreases the value by 1.
–b is called a pre-decrement operator. It first decrements the value of the variable or operand by 1 and then uses the variable.

Assignment Operators in JavaScript

These operators are used to assign values to a variable or operand.
The assignment operators are:

Assignment operator (=)

It is used to initialize a variable with some value.

let str = "Hello";

Add and assign operator (+=)

It is used to perform an addition operation on a variable and then assign the value in the same variable itself.

let num=24;
num+=3;
console.log(num); // 27

num+=3 is equivalent to num=num+3. Thus, num+=3 is a shorthand for num=num+3.

Subtract and Assign (-=)

It is used to perform a subtraction operation on a variable and then assign the computed value in the same variable itself.

let num=20;
num-=2;
console.log(num); // 18

num-=2 is equivalent to num=num-2. Thus, num-=2 is a shorthand for num=num-2.

Multiply and assign (*=)

It is used to perform a multiplication operation on a variable and then assign the computed value in the same variable itself.

let num=4.3;
num*=3;
console.log(num); // 12.9

num*=3 is equivalent to num=num*3. Thus, num*=3 is a shorthand for num=num*3.

Divide and assign (/=)

It is used to perform a division operation on a variable and then assign the value in the same variable itself.

let num=24;
num/=6;
console.log(num); // 4

num/=4 is equivalent to num=num/4. Thus, num/=4 is a shorthand for num=num/4.

Modulus and assign (*=)

It is used to perform a modulus operation on a variable and then assign it to the same variable.

let num=13;
num%=5;
console.log(num); // 3

num%=5 is equivalent to num=num%5. Thus, num%=5 is a shorthand for num=num%5.

Comparison operators in JavaScript

These operators are used to compare two values or operands.
The comparison operators are:

== (Equal to)

It is used to compare the values of two or more variables and returns true if two values are equal else it returns false.

let a=8,b=8,c=4;
console.log(a==b); // true
console.log(a==c); // false
console.log(a=='8'); // true

=== (Strictly Equality Comparison Operator or Identical)

It is used to compare the value and type of a variable or operand and returns true if two operands values and type are equal otherwise it returns false.

let a=8,b=8,c=4;
console.log(a===c); // false
console.log(a===b); // true
console.log(a==='8'); // false

!= (Not equal to)

It returns true if the values of two operands are not equal otherwise it returns false.

let a='Hello';
let b='Hii';
let c='Hii';
console.log(a!=b); // true
console.log(a!=c); // false
console.log(4!='4'); // false

!== (Not identical)

It returns true if the values and type of two operands are not equal otherwise it returns false.

let a='5',b=5,c=5;
console.log(a!==b); // true
console.log(b!==c); // false

> (Greater than)

It returns true if the value of the first value is greater than the second value otherwise false.

console.log(5>4); // true
console.log(-6>-4); // false

< (Less than)

It returns true if the value of the first value is less than the second value otherwise it returns false.

console.log(2<3); // true
console.log(-5<-7); // false

>= (Greater than or equal to)

It returns true if the value of the first value is greater than or equal to the second value otherwise it is false.

console.log(5>=5); // true
console.log(7>=4); // true
console.log(-3>=9); // false

<= (Less than or equal to)

It returns true if the value of the first value is less than or equal to the second value otherwise it is false.

console.log(3<=3); // true
console.log(-1<=4); // true

Logical Operators in JavaScript

These are the operators used to perform logical operations.
The logical operators are:

&& (Logical AND)

It returns true if both operands or conditions are true or non-zero otherwise it returns false.

console.log((5=='5') && (2>=3)); // false

Here, 5==’5′ evaluates to true and 2>=3 evaluates to false. Finally, true and false evaluates to be false.

|| (Logical OR)

It returns true if either one of the operands or conditions is true or non-zero otherwise it returns false.

console.log((5=='5') || (2>=3)); // true

Here, 5==’5′ evaluates to true and 2>=3 evaluates to false. Finally, true and false are evaluated to be true.

! (Logical NOT)

It reverses the result of a condition or operand. It returns true if the condition is false otherwise it returns false.

console.log(!(true)); // false
console.log(!(3<-4)); // true

Bitwise Operators in JavaScript

It is used to perform bitwise i.e., bit-by-bit operations on operands. It works on the bit.

& (Bitwise AND)

It returns 1 if the corresponding bits of two operands are 1 else 0.

let a=13,b=4;
console.log(a & b); // 4

Here, a=13 and b=4 in binary format are 1101 and 0100 respectively. After performing bitwise AND operation, we will get 0100 which is equivalent to 4 in decimal form.

| (Bitwise OR)

It returns 1 if either of the corresponding bits of two operands is 1, or else 0.

let a=10,b=3;
console.log(a | b); // 11

Here, a=10 and b=3 in binary format are 1010 and 0011 respectively. After performing bitwise OR operation, we will get 1011 which is equivalent to 11 in decimal form.

^ (Bitwise XOR)

It returns 1 if the corresponding bits of two operands are not the same or opposite else 0.

let a=15,b=5;
console.log(a ^ b); // 10

Here, a=15 and b=5 in binary format are 1101 and 0101 respectively. After performing bitwise XOR operation, we will get 1010 which is equivalent to 10 in decimal form.

~ (Bitwise NOT)

It returns 1 if the bit of operand is 0 and 0 if bit of operand is 1.

let a=2;
console.log(~a); // -3
~a=-a-1

Here, a=2 in binary format is 0010 After performing bitwise AND operation, we will get 1011 which is equivalent to -3 in decimal form.

<< (Bitwise left shift)

It is used to shift the bits of operands towards the left by a specified number of bits.

console.log(2<<2); // 8

Here, 2=0010 in binary format. After shifting 2 to 2 places towards the left, the result is 8.

>> (Bitwise right shift)

It is used to shift the bits of operands towards the right by a specified number of bits.

console.log(15>>3); // 1

Here, 15=1111 in binary format. After shifting 15 to 3 places towards the right, the result is 1.

Ternary Operator in JavaScript

It is a conditional operator. It is used to assign a value to a variable depending upon the condition. It is denoted by ?:.
Syntax: variable = (expression1) ? expression2 : expression3
If expression1 evaluates to be true then expression2 is evaluated and its value is assigned to a variable, otherwise expression3 is evaluated and its value is assigned to a variable,

let a=(5>4)?5 : 4;
console.log(a); // 5

typeof operator in JavaScript

The typeof operator basically checks the data type of an operand.

let name="JavaScript";
console.log(typeof name); // string
console.log(typeof 5.6); // number

I hope the Types of Operators in JavaScript module are beneficial for you and that now you know what operators are in JavaScript along with their types. Thank you for reading.