JavaScript includes operators same as other languages. An operator performs some operation on single or multiple operands (data value) and produces a result. For example, in 1 + 2 , the + sign is an operator and 1 is left side operand and 2 is right side operand.
JavaScript Operator Types
Here is a list of different operators you will learn in this tutorial.
- Assignment Operators
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
1. Assignment Operators:
Assignment operators are used to assign values to different variables. For example,
let x = 20;
Equal to =
operator is used to assign value 20 to variable X.
list of commonly used assignment operators:
Operator | Name | Example |
---|---|---|
= | Assignment operator | a = 5; // 5 |
+= | Addition assignment | a += 5; // a = a + 5 |
-= | Subtraction Assignment | a -= 20; // a = a - 20 |
*= | Multiplication Assignment | a *= 30; // a = a * 30 |
/= | Division Assignment | a /= 5; // a = a / 5 |
%= | Remainder Assignment | a %= 10; // a = a % 10 |
**= | Exponentiation Assignment | a **= 2; // a = a**2 |
2. Arithmetic Operators:
It is used to perform arithmetic calculations. For example,
const total = 5 + 5; // 10
Plus +
operator is used to add two operands.
list of commonly used arithmetic operators:
Operator | Name | Example |
---|---|---|
+ | Addition | x + y |
- | Subtraction | x - y |
* | Multiplication | x * y |
/ | Division | x / y |
% | Remainder | x % y |
++ | Increment (increments by 1) | ++x or x++ |
-- | Decrement (decrements by 1) | --x or x-- |
** | Exponentiation (Power) | x ** y |
3. Comparison Operators:
It compare two values and return a boolean value, either true
or false
. For example,
const x = 5, x = 4;
console.log(x > y); // true
const a = 5, b = 10;
console.log(a > b); // false
Greater than operator >
is used to compare whether x is greater than y.
list of commonly used Comparison operators:
Operator | Description | Example |
---|---|---|
== | Equal to: returns true if the operands are equal | x == y |
!= | Not equal to: returns true if the operands are not equal | x != y |
=== | Strict equal to: true if the operands are equal and of the same type | x === y |
!== | Strict not equal to: true if the operands are equal but of different type or not equal at all | x !== y |
> | Greater than: true if left operand is greater than the right operand | x > y |
>= | Greater than or equal to: true if left operand is greater than or equal to the right operand | x >= y |
< | Less than: true if the left operand is less than the right operand | x < y |
<= | Less than or equal to: true if the left operand is less than or equal to the right operand | x <= y |
4. Logical Operators :
Logical operators perform logical operations and return a boolean value, either true
or false
. For example,
const a = 5, b = 10;
(a < 10) && (a < 5); // true
// logical AND
console.log(true && true); // true
console.log(true && false); // false
// logical OR
console.log(true || false); // true
// logical NOT
console.log(!true); // false
&&
is the logical operator AND. Since both x < 6
and y < 5
are true
, the result is true
.
Operator | Description | Example |
---|---|---|
&& | Logical AND: true if both the operands are true , else returns false | x && y |
|| | Logical OR: true if either of the operands is true ; returns false if both are false | x || y |
! | Logical NOT: true if the operand is false and vice-versa. | !x |
5. Bitwise Operators :
Bitwise operators perform operations on binary representations of numbers.
- The four most critical bitwise operators present in JavaScript are:
- Bitwise And
- Bitwise Or
- Bitwise Not
- Bitwise XOR
Operator | Description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Left shift |
>> | Sign-propagating right shift |
>>> | Zero-fill right shift |
- An Operator performs some operation on single or multiple operands (data value) and produces a result.
- Assignment operators are used to assign values to different variables.
- Arithmetic used to perform arithmetic calculations.
- Comparison Operators compare two values and return a boolean value, either
true
orfalse
. - Logical Operators perform logical operations and return a boolean value, either
true
orfalse
. - Bitwise operators perform operations on binary representations of numbers.