Operators in JavaScript are symbols used to perform operations on variables and values. They are essential for writing logic, calculations, and conditions
1. Arithmetic Operators
Used for mathematical calculations.
| Operator | Description | Example |
|---|---|---|
+ | Addition | 5 + 2 = 7 |
- | Subtraction | 5 - 2 = 3 |
* | Multiplication | 5 * 2 = 10 |
/ | Division | 10 / 2 = 5 |
% | Modulus (remainder) | 5 % 2 = 1 |
** | Exponentiation | 2 ** 3 = 8 |
Example:
let a = 10;
let b = 3;console.log(a + b); // 13
console.log(a % b); // 1
2. Assignment Operators
Used to assign values to variables.
| Operator | Example | Meaning |
|---|---|---|
= | x = 5 | Assign 5 |
+= | x += 3 | x = x + 3 |
-= | x -= 2 | x = x – 2 |
*= | x *= 2 | x = x * 2 |
/= | x /= 2 | x = x / 2 |
Example:
let x = 5;
x += 3; // 8
3. Comparison Operators
Used to compare two values (returns true/false).
| Operator | Description |
|---|---|
== | Equal (value only) |
=== | Strict equal (value + type) |
!= | Not equal |
!== | Strict not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
Example:
console.log(5 == "5"); // true
console.log(5 === "5"); // false
4. Logical Operators
Used to combine conditions.
| Operator | Description |
|---|---|
&& | AND |
| ` | |
! | NOT |
Example:
let age = 20;if (age > 18 && age < 30) {
console.log("Young adult");
}
5. Unary Operators
Operate on a single operand.
| Operator | Description |
|---|---|
++ | Increment |
-- | Decrement |
typeof | Returns type |
! | Logical NOT |
Example:
let num = 5;
num++; // 6console.log(typeof num); // number
6. Ternary Operator
Short form of if-else.
Syntax:
condition ? value1 : value2;
Example:
let age = 18;
let result = age >= 18 ? "Adult" : "Minor";
7. Special Operators
Some useful additional operators:
| Operator | Description |
|---|---|
?. | Optional chaining |
?? | Nullish coalescing |
in | Check the property in the object |
instanceof | Check object type |
Example:
let user = {};
console.log(user?.name); // undefined (no error)let value = null ?? "default";
console.log(value); // "default"
✅ Summary
- Arithmetic → calculations
- Assignment → assign values
- Comparison → compare values
- Logical → combine conditions
- Ternary → short if-else
- Special → modern JS features