JavaScript provides different type of operations same as other programming languages. There are some additional operators which make javascript more suitable for programming.
Javascript supports the following types of operators:
Index
They are used to perform different arithmetic operations between two or more variables.
Operator | Description | Example: (here m = 8, n = 2 ) |
---|---|---|
+ | Addition | var x = m + n // Ans: 10 |
– | Subtraction | var x = m – n // Ans: 6 |
* | Multiplication | var x = m * n // Ans: 16 |
/ | Division | var x = m / n // Ans: 4 |
% | Modulus | var x = m % n // Ans: 0 |
They are used to perform logical operations like AND Operation, OR Operation and NOT operations between two or more variables.
Operator | Description | Example: (here m = 8, n = 2 ) |
---|---|---|
&& | AND | var x = (m > 0) && (n < 5) // Ans: true |
|| | OR | var x = (m == 8) || (n < 0) // Ans: true |
! | NOT | var x = !(n == m – 6) // Ans: false |
NOTE:
They are created to compare two or more variables. They are mostly used with logical operators.
Operator | Description | Example: (here m = 8, n = 2 , int) |
---|---|---|
> | greater than | var x = m > n // Ans: true |
< | less than | var x = m < 5 // Ans: false |
>= | Greater than Or Equal to | var x = m >= n // Ans: true |
<= | less than Or equal to | var x = n <= 2 // Ans: true |
== | Equal to | var x = (m == n) // Ans: false |
=== | Equal to with Equal type | var x = (m === 8) // Ans: true |
!= | Not Equal to | var x = m != n // Ans: true |
!== | Not Equal to with Not Equal type | var x = m !== 5 // Ans: true |
They are used to increment and decrement the values.
Operator | Description |
---|---|
++ | Increment |
— | Decrement |
Note: Based on the position they are used, we can divide them into two types.
Example: (m =8 , n = 2)
Pre increment
var x = m++; // Ans: x = 8 , m = 9
Post increment
var x = ++n // Ans: x = 3 , n = 3
Example: (m =8 , n = 2)
Pre decrement
var x = m–; // Ans: x = 8 , m = 7
Post decrement
var x = –n // Ans: x = 1 , n = 1
They are also called ternary operators. According to the condition, the result value is assigned to a variable.
Syntax:
Variable name = ( condition ) ? val1 : val2
Explanation:
If the condition is true, then val1 is assigned to the variable; otherwise, val2 is assigned to the variable.
Example: (m =8 , n = 2)
var x = (m > 5) ? 7 : 3 // Ans: x = 7
var x = (n == 5) ? 7 : 3 // Ans: x = 3
JavaScript supports different bitwise operators. During these operations, data is converted into a 32-bit format, and then operations are performed, again the result is converted back to javascript number.
Operator | Description | Example | Same as | Result | Decimal |
---|---|---|---|---|---|
& | AND | x = 4 & 1 | 0100 & 0001 | 0000 | 0 |
| | OR | x = 4 | 1 | 0100 | 0001 | 0101 | 5 |
~ | NOT | x = ~ 4 | ~0100 | 1011 | 11 |
^ | XOR | x = 4 ^ 1 | 0100 ^ 0001 | 0101 | 5 |
<< | Left shift | x = 4 << 1 | 0100 << 1 | 1000 | 8 |
>> | Right shift | x = 4 >> 1 | 0100 >> 1 | 0010 | 2 |
They are used to concatenate two or more strings in javascript.
Example 1:
var e1 = “I am”;
var e2 = “ Errorsea”;
var e3 = e1 + e2; // Ans: “I am Errorsea”
Example 2:
var e1 = “I am”;
var e2 = “ Errorsea”;
var e1 += e2; // Ans: “I am Errorsea”
It returns the type of a variable.
Example:
type of “Errorsea” // Ans: string
typeof 200 // Ans: number
typeof NaN // Ans: number
typeof true // Ans: boolean
typeof [1, 2, 4, 7] // Ans: object
typeof {name:’Errorsea’, age:18} // Ans: object
typeof new Date() // Ans: object
typeof null // Ans: object
typeof Bike // Ans: undefined (if Bike is not declared)
typeof function () {} // Ans: function
It used to delete properties of objects in javascript.
Example:
var person = {firstName:”Nick”, age:18, Color:”white”};
delete person[“Color”];
The above code deletes the Color property from the object person.
Note: We cannot use the deleted property in the code without adding it again.
It is used to check the specified property in the object.
This operator returns true if the specified property is present in the object.
Example:
var person = {firstName:”Nick”, age:18, Color:”white”};
var x = age in person; // Ans: true
var x = Birthdate in person; //Ans: false
It is used for checking whether the specified object is an instance of in the object or not. This operator returns true if the specified property is an instance of the object.
Example:
var media = [“facebook”, “whatsapp”, “instagram”];
media instanceof Array; // Ans: true
media instanceof String; // Ans: false
media instanceof Object; // Ans: true
media instanceof Number; // Ans: false
This operator is mostly used to get undefined primitive value using void(0) in javascript. It evaluates the expression and returns undefined.
Example:
<a href=”javascript:void(0)”>I am useless link</a>
It is used to assign value to the Javascript variable.
It is denoted by ‘ = ’ sign.
Example:
var m = “Errorsea”;
var n = 5;
Note: Javascript also supports shorthand notations with assignment operators.
Types of Assignment Operators
Operations | Description | Same as (here m = 8, n = 2 ) |
---|---|---|
m = n | assignment | m = n // Ans: m=2 |
m += n | Addition | m = m + n // Ans: m=10 |
m -= n | Subtraction | m = m – n // Ans: m=6 |
m *= n | Multiplication | m = m * n // Ans: m=16 |
m /= n | Division | m = m / n // Ans: m=4 |
m %= n | Modulus | m = m % n // Ans: m=0 |
These are the different types of operators used in javascript according to the requirements for providing users the better facilities. User can take actions according to the result of different operators.
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…