JAVASCRIPT OPERATORS
What are Operators in Javascript?
Operators are symbols that define what kind of operation is to be performed on
operators. For example, the symbol of addition (+)
tells javascript engine to add given
operators.
There are different types of operator in javascript mentioned below:
- Arithmetic Operators
- Comparison Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Special Operators
1. Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on numbers. These are the following operators with their use. Check here for detailed study.
Operators | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
** | Exponential (ES6) |
% | Modulus used to find remainder |
/ | Division |
++ | Increment operation used to increase number by 1 |
-- | Decrement operation used to decrease number by 1 |
Now let's see an example of how to use these arithmetic operators.
2. Comparison Operators
Comparison operator is used to compare two operators. These operators return
true
or false
values on the basis of result.
Lets see the comparison operators in the table with description.
Operators | Description |
---|---|
== | equals to operator (returns true when both operators are equal) |
=== | Identical to operator (returns true when both operators are equal and of same data type)
|
> | greater than operator (returns true when left operator is greater than right operator)
|
< | less than operator (returns true when left operator is lesser than right operator)
|
>= | greater than equals to operator (returns true when left operator is greater than or equal
right operator) |
<= | less than equals to operator (returns true when left operator is less than or equal right
operator)
|
!= | Not equals to operator (returns true when operators are not equal) |
!== | Not identical to operator (returns true when operators are either not equal or of
different type) |
Lets see an example to visualise the code and result.
3. Assignment Operators
Assignment operator is used to assign and change values of the operators. For
example, a = 10;
now a is equal to 10.
In assignment operators can also do arithmetic operations and then assign results to operators. For example,
let a = 10; a += 10
now a become 20 as a += 10
is equivalent to
a = a + 10
.
These are the following assignment operators in javascript.
Operators | Description |
---|---|
= | Used to assign given value |
+= | Used to add and assign result |
-= | Used to subtract and assign result |
*= | Used to multiply and assign result |
**= | Used to get exponential and assign result |
%= | Used to get modulus and assign result |
/= | Used to divide and assign result |
Lets see the example.
4. Logical Operators
Logical operators perform logical operations in javascript. For example, &&
operation true && true = true
.
Operators | Description |
---|---|
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
Lets see the example.
5. Bitwise Operators
Bitwise operators are the operators that are used to change bit values of the operands. Bitwise operators treat their operands as a sequence of 0's and 1's of 32 bits.
These are the following bitwise operators in javascript.
Operator | Name | Description |
---|---|---|
& | AND | give 1 only when both bits are 1. ie 1 & 1 = 1 |
| | OR | give 0 only when both bits are 0. ie 0 | 1 = 1 and 0 | 0 = 0 |
^ | XOR | give 1 only when one bits is 1. ie 1 & 1 = 0 and 0 ^ 1 = 1 |
~ | NOT | Reverse the bit value. ie ~ 1 = 0 |
<< | Left Shift(zero fill) | Shift all bit values to left by the given number and fill 0 in rightmost. ie 5 << 1=10 |
>> | Right Shift (sign preserving) | Shift all bit values to right by the given number and preserving sign. ie -10 >> 1 = -5 |
>>> | Right Shift (zero fill) | Shift all bit values to let by the given number and fill 0 in rightmost. ie 15 >>> 1=7 |
Lets see an example.
6. Special Operators
Special operators serve special purposes. These following operators lie under special operators categories..
Operators | Description |
---|---|
( ? : ) | Ternary operator. It is used for if else purposes. |
delete | used to delete object's property |
in | Used to check if object has given property |
new | Creates new instance of a class(object) |