What is an Operator?
In JavaScript, an operator is a special symbol used to perform operations on operands (values and variables). For example,
2+3; // 5
Here + is an operator that performs addition, and 2 and 3 are operands.
Types of JavaScript Operators :
There are different types of operators in JavaScript that are used for performing different operations. Some of the JavaScript Operators include:
- Assignment Operators
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
Assignment Operators :
Assignment operators are used to assigning values to variables. For example,
Here, the = operator is used to assign value 5 to variable x.
Operator | Description | Example |
= | Assigns values from the right side operand to the left side operand | 20+10 = 30 |
+= | It adds the right operand to the left operand and assigns the result to the left operand | var a=20; a+=10; Now a = 30 |
-= | It subtracts the right operand from the left operand and assigns the result to the left operand | var a=30; a-=10; Now a = 20 |
*= | It multiplies the right operand with the left operand and assigns the result to the left operand | var a=10; a*=20; Now a = 200 |
/= | It divides the left operand with the right operand and assigns the result to the left operand | var a=10; a/=2; Now a = 5 |
%= | It takes modulus using two operands and assigns the result to the left operand | var a=10; a%=2; Now a = 0 |
Arithmetic Operators:
Arithmetic operators are used to perform arithmetic operations on the operands. Here is a list of operators that are known as JavaScript arithmetic operators:
For Example:
Operator | Description | Example |
+ | Adds two operands | 10 + 20 = 30 |
– | Subtracts the second operand from the first | 30 – 20 = 10 |
/ | Divide the numerator by the denominator | 20/10 = 2 |
* | Multiply two operands | 5 * 5 = 25 |
% | Outputs the remainder of an integer division | 20 % 10 = 0 |
++ | Increases an integer value by one | var a=20; a++; Now a = 21 |
— | Decreases an integer value by one | var a=20; a–; Now a = 19 |
Example :
Comparison Operators :
The JavaScript comparison operator compares the two operands. The comparison operators are as follows:
For Example:
Operator | Description | Example |
== | Checks if two operands are equal or not. If yes, then the condition becomes true. | 20==30 = false |
=== | Finds the identical (equal and of the same type) | 10==20 = false |
!= | Checks if two operands are equal or not. If the values are not equal, then the condition becomes true | 20!=30 = true |
!== | It implies that two values are not Identical | 20!==20 = false |
> | Checks if the value of the left operand is greater than the value of the right operand | 30>10 = true |
>= | Checks if the value of the left operand is greater than or equal to the value of the right operand | 20>=10 = true |
< | This Checks if the value of the left operand is less than the value of the right operand | 20<10 = false |
<= | Checks if the value of the left operand is less than or equal to the value of the right operand | 30<=10 = false |
Example:
Logical Operators :
Logical operators perform logical operations and return a boolean value, either true or false. For example,
Here, && is the logical operator AND. Since both x < 6 and y < 5 are true, the result is true.
Operator | Description | Example |
&& | Logical AND – If both the operands are non-zero, then the condition becomes true | (10==20 && 20==33) = false |
|| | Logical OR – If any of the two operands are non-zero, then the condition becomes true. | (10==20 || 20==33) = false |
! | Logical NOT – Reverses the logical state of its operand. | !(10==20) = true |
Example:
Bitwise Operators :
The bitwise operators are used to perform bitwise operations on operands. Here is a list of bitwise operators:
Operator | Description | Example |
& | Boolean AND operation on each bit of its integer arguments | (10==20 & 20==33) = false |
| | It performs a Boolean OR operation on each bit of its integer arguments | (10==20 | 20==33) = false |
^ | This operator performs Bitwise XOR operation | (10==20 ^ 20==33) = false |
~ | It is a unary operator and operates by reversing all the bits in the operand | (~10) = -10 |
<< | Moves all the bits in its first operand to the left by the number of places specified in the second operand. | (10<<2) = 40 |
>> | The left operand’s value is moved right by the number of bits specified by the right operand. | (10>>2) = 2 |
>>> | This operator is just like the >> operator, except that the bits shifted in on the left are always zero. | (10>>>2) = 2 |