Learning JavaScript operators is quite essential for you if you want to work with the JavaScript programming language. This tutorial talks about different types of JavaScript operators.
What are JavaScript Operators?
JavaScript operators are the collection of operators or symbols that perform certain operations on the operands. In any JavaScript program, operators play a major role in manipulating operands by exercising a wide range of operations.
Additionally, there are several different types of JavaScript operators that you should be aware of.
Examples:
var addition = 10 + 5; // Here “+” is the operator, and “10” and “5” are operands
var subtraction = 10 – 5; //Here “-” is the operator, and “10” and “5” are operands
Types of JavaScript Operators
There are six types of JavaScript operators that are as follows:
- Assignment Operators
- Arithmetic Operators
- Logical Operators
- Comparison Operators
- Bitwise Operators
- Special Operators
Let us now discuss each type of JavaScript operator in detail.
1. Assignment Operators
With assignment operators, we can assign or manipulate the values of operands. The following table lists all the assignment operators:
Operator | Description | Example |
= | Assign | var x = 10; |
+= | Add and Assign | var x=10;
x+=5; // x = x + 5 i.e. x = 15 |
-= | Subtract and Assign | var x=10;
x-=5; // x = x – 5 i.e. x = 5 |
*= | Multiply and Assign | var x=10;
x*=5; // x = x * 5 i.e. x = 50 |
/= | Divide and Assign | var x=10;
x/=5; // x = x / 5 i.e. x = 2 |
%= | Modulus and Assign | var x=10;
x%=5; // x = x % 5 i.e. x = 0 (Remainder) |
**= | Exponentiate and Assign | var x=10;
x**=2; // x = x ** 2 i.e. x = 100 |
2. Arithmetic Operators
With Arithmetics operators, we can perform various arithmetic operations on operands. The following table lists all the arithmetic operators in JavaScript:
Operator | Description | Example |
+ | Addition | var x = 10 + 5; |
– | Subtraction | var x = 10 – 5; |
* | Multiplication | var x = 10 * 5; |
/ | Division | var x = 10 / 5; |
% | Modulus | var x = 10 % 5; |
++ | Increment | var x = 10;
x++; // x = 11 |
— | Decrement | var x = 10;
x–; // x = 9 |
** | Exponentiation | var x = 10**2; // x=100 |
3. Logical Operators
With Logical operators, we can perform various logical operations. The output of a logical operation is a Boolean value, i.e., true or false. The following table highlights all the logical operators in JavaScript:
Operator | Description | Example |
&& | Logical AND | (5==10 && 10==10); // returns true |
|| | Logical OR | (5==10 || 10==10); // returns false |
! | Logical NOT | !(5==10); // returns true |
4. Comparison Operators
With Comparison operators, we can compare two operands, and these operators are of great importance for implementing loops. The following table consists of all the comparison operators in JavaScript:
Operator | Description | Example |
== | Equal to | 10==10; // returns true |
!= | Not equal to | 10!=10; // returns false |
=== | Strictly equal to (compares value and type of operands, and both must be true) | 10===”10″; // returns false |
!== | Strictly not equal to (compares value and type of operands, and any parameter can be false) | 10!==”10″; // returns true |
> | Greater than | 10>20; // returns false |
>= | Greater than or equal to | 20>=10; // returns true |
< | Less than | 10<20; // returns true |
<= | Less than or equal to | 20<=10; // returns false |
5. Bitwise Operators
With Bitwise operators, we can perform various bitwise operations on operands. Also, bitwise operators work with 32-bit numbers, and thus during a bitwise operation, each number is converted into its corresponding 32-bit number. Also, after the operation is completed, the result, which is a 32-bit number, is converted back into decimal form. The following table lists all the bitwise operators in JavaScript:
Operator | Description | Example |
& | Bitwise AND | 3 & 9; // returns 1 |
| | Bitwise OR | 3 | 9; // returns 11 |
^ | Bitwise XOR | 3 ^ 9; // returns 10 |
~ | Bitwise NOT | ~3; // returns -4 |
<< | Bitwise Left Shift | 3 << 9; // returns 1536 |
>> | Bitwise Right Shift | 3 >> 9; // returns 0 |
>>> | Bitwise Right Shift with Zero | 3>>>9; // returns 0 |
6. Special Operators
There are several special operators in JavaScript that you may need to use in certain scenarios. The following table highlights various special operators in JavaScript:
Operator | Description | Example |
, | It evaluates multiple expressions or operands and returns the last operand | x = (3, 9); // returns 9 |
?: | It is like the if-else statement that returns one of the two values depending on the specified condition | (5<10) ? “true”:”false”); // returns true |
typeof | Checks and returns the type of an operand | typeof (3); // returns number |
instanceof | Checks if the specified object is of a particular object type | var name = new String(“John”);
name instanceof String; // returns true |
delete | Deletes an element of an array or a property of an object | var array = [1,2,3,4];
delete array[3]; const Fullname = { firstName: ‘John’, lastName: ‘Doe’ }; delete Employee.firstName; |
in | Checks if the specified property is in an object | const Employee = {
firstname: ‘John’, lastname: ‘Doe’ }; ‘firstname’ in Employee; // returns true |
void | Discards the return value of an expression. | var x = 5;
void (x); // returns undefined |
Takeaway
As a JavaScript developer, it is essential for you to become familiar with all the important JavaScript operators. At times, when you write JavaScript code or programs, you need to perform various operations on variables or operands to make sure that the overall code or program behaves as intended.
People Who Read This Article Also Read:
Hi! I am Pankaj, a full-time content specialist and a part-time programmer and marketer. I love to explore new places and also new ideas. I am an inquisitive person with a passion to learn new skills through every possible opportunity that comes in the way.