
An operator is used for manipulating a certain value or operand. Operators are used to perform specific mathematical and logical computations on operands. In this article, we will discuss about different JavaScript Operators and how they are used in a code in the following sequence:
What is an Operator?
Operators are used for comparing values, perform arithmetic operations, etc. For example, if we take a simple expression, 4 + 5 is equal to 9. Here 4 and 5 are called operands and ‘+’ is called the operator. JavaScript consists of different types of operators that are used to perform different operations.
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:
- Arithmetic Operators
- Comparison Operators
- Bitwise Operators
- Logical Operators
- Assignment Operators
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:
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 |
Comparison Operators
The JavaScript comparison operator compares the two operands. The comparison operators are as follows:
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 |
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 |
Logical Operators
The list provides all the JavaScript logical operators:
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 |
Assignment Operators
The Assignment operators are used to assign values to the operand. The following operators are known as JavaScript assignment operators:
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 |
These were some of the common JavaScript operators with their definition and example. With this, we have come to the end of our article.
Now that you know about JavaScript Operators, check out the web developer course online by Edureka. Web Development Certification Training will help you Learn how to create impressive websites using HTML5, CSS3, Twitter Bootstrap 3, jQuery and Google APIs and deploy it to Amazon Simple Storage Service(S3).
Got a question for us? Please mention it in the comments section of “JavaScript Operator” and we will get back to you.
Upcoming Batches For Web Developer Certification Training Course