Last Updated on July 21, 2023 by Mayank Dham
JavaScript operators are fundamental building blocks of the language, allowing developers to perform various operations on values. From basic arithmetic calculations to complex logical evaluations, JavaScript operators provide the necessary tools to manipulate data and control program flow. In this article, we will explore the different types of JavaScript operators, their precedence, and usage scenarios. Whether you’re a beginner or an experienced developer, understanding JavaScript operators is essential for writing efficient and powerful code.
Different Types of JavaScript Operators
There are the following types of operators in JavaScript.
- Arithmetic Operators
- Comparison (Relational) Operators
- Bitwise Operators
- Logical Operators
- Assignment Operators
- Special Operators
Arithmetic Operators:
To perform arithmetic operations on the operands, arithmetic operators are used. JavaScript arithmetic operators are the operators listed below.
Operator | Description | Example |
---|---|---|
+ | Addition | 10+20 = 30 |
– | Subtraction | 20-10 = 10 |
* | Multiplication | 10*20 = 200 |
/ | Division | 20/10 = 2 |
% | Modulus (Remainder) | 20%10 = 0 |
++ | Increment | var a=10; a++; Now a = 11 |
— | Decrement | var a=10; a–; Now a = 9 |
Comparison (Relational) Operators:
The comparison operator in JavaScript compares the two operands. The following are the comparison operators:
Operator | Description | Example |
---|---|---|
\== | Is equal to | 10==20 = false |
\=== | Identical (equal and of same type) | 10==20 = false |
!= | Not equal to | 10!=20 = true |
!== | Not Identical | 20!==20 = false |
> | Greater than | 20>10 = true |
>= | Greater than or equal to | 20>=10 = true |
< | Less than | 20<10 = false |
<= | Less than or equal to | 20<=10 = false |
Bitwise Operators:
Bitwise operators apply bitwise operations to operands. The following are the bitwise operators:
Operator | Description | Example | |
---|---|---|---|
& | Bitwise AND | (10==20 & 20==33) = false | |
I | Bitwise OR | (10==20 | 20==33) = false |
^ | Bitwise XOR | (10==20 ^ 20==33) = false | |
~ | Bitwise NOT | (~10) = -10 | |
<< | Bitwise Left Shift | (10<<2) = 40 | |
>> | Bitwise Right Shift | (10>>2) = 2 | |
>>> | Bitwise Right Shift with Zero | (10>>>2) = 2 |
Logical Operators:
JavaScript logical operators include the following operators.
Operator | Description | Example | ||
---|---|---|---|---|
&& | Logical AND | (10==20 && 20==33) = false | ||
Logical OR | (10==20 | 20==33) = false | ||
! | Logical Not | !(10==20) = true |
Assignment Operators:
JavaScript assignment operators include the following operators.
Operator | Description | Example |
---|---|---|
\= | Assign | 10+10 = 20 |
+= | Add and assign | var a=10; a+=20; Now a = 30 |
-= | Subtract and assign | var a=20; a-=10; Now a = 10 |
*= | Multiply and assign | var a=10; a*=20; Now a = 200 |
/= | Divide and assign | var a=10; a/=2; Now a = 5 |
%= | Modulus and assign | var a=10; a%=2; Now a = 0 |
Special Operators:
JavaScript special operators include the following operators.
Operator | Description |
---|---|
(?:) | Based on the condition, the Conditional Operator returns a value. It is similar to if-else. |
, | Multiple expressions can be evaluated as a single statement using the Comma Operator. |
delete | The Delete Operator is used to remove a property from an object. |
in | Operator determines whether an object has the specified property. |
instanceof | determines whether the object is an instance of the specified type |
new | creates an instance (object) |
typeof | checks the type of object. |
void | It discards the expression’s return value. |
yield | inspects the output of a generator’s iterator. |
Conclusion
JavaScript operators are vital tools for performing calculations, making decisions, and manipulating data in JavaScript programs. By understanding and mastering the various types of operators, you gain the ability to write more efficient and expressive code. From arithmetic and assignment operators to comparison, logical, and bitwise operators, each operator type serves a specific purpose and plays a crucial role in JavaScript development. As you continue your journey in JavaScript, embracing operators will empower you to create sophisticated algorithms, optimize code, and build robust applications.
Frequently Asked Questions (FAQs)
Here are some of the most Frequently Asked Questions related to JavaScript Operators
Q1. What are the four types of operators in JavaScript?
The four types of operators in JavaScript are arithmetic operators, assignment operators, comparison operators, and logical operators.
Q2. What is a JavaScript operator?
In JavaScript, an operator is a symbol or combination of symbols that perform operations on operands. They are used to manipulate values, perform calculations, compare values, and more.
Q3. What are the three types of operators in JavaScript?
The three types of operators in JavaScript are arithmetic operators, comparison operators, and logical operators.
Q4. What are an operator and an expression in JavaScript?
An operator in JavaScript is a symbol that performs a specific operation on one or more operands. An expression is a combination of values, variables, and operators that evaluates to a single value.
Q5. What does "===" mean in JavaScript?
In JavaScript, "===" is the strict equality operator. It compares two values for equality, both in terms of value and data type. It returns true if the values are equal and of the same type, and false otherwise.