JavaScript Operators
2 April 2025 | Category: JavaScript
JavaScript operators are symbols that perform operations on variables and values. Operators are used to manipulate data and perform calculations.
1️⃣ Types of JavaScript Operators
JavaScript has several types of operators:
Type | Example | Description |
---|---|---|
Arithmetic | + , - , * , / , % , ** | Perform mathematical operations |
Assignment | = , += , -= , *= , /= , %= , **= | Assign values to variables |
Comparison | == , === , != , !== , > , < , >= , <= | Compare values |
Logical | && , ` | |
Bitwise | & , ` | , ^, ~, <<, >>, >>>` |
Ternary | condition ? trueValue : falseValue | Shorter if-else statement |
Type Operators | typeof , instanceof | Check variable type |
2️⃣ Arithmetic Operators 🧮
Used for basic mathematical calculations.
Operator | Example | Description |
---|---|---|
+ | 10 + 5 | Addition |
- | 10 - 5 | Subtraction |
* | 10 * 5 | Multiplication |
/ | 10 / 5 | Division |
% | 10 % 3 | Modulus (Remainder) |
** | 2 ** 3 | Exponentiation (Power) |
🔹 Example:
let a = 10, b = 5;
console.log(a + b); // Output: 15
console.log(a % b); // Output: 0
console.log(2 ** 3); // Output: 8 (2^3)
3️⃣ Assignment Operators 🎯
Used to assign values to variables.
Operator | Example | Equivalent to |
---|---|---|
= | x = 10 | Assign 10 to x |
+= | x += 5 | x = x + 5 |
-= | x -= 5 | x = x - 5 |
*= | x *= 5 | x = x * 5 |
/= | x /= 5 | x = x / 5 |
%= | x %= 3 | x = x % 3 |
**= | x **= 2 | x = x ** 2 |
🔹 Example:
let x = 10;
x += 5; // x = x + 5
console.log(x); // Output: 15
4️⃣ Comparison Operators 🔄
Used to compare two values.
Operator | Example | Description |
---|---|---|
== | 5 == "5" | Equal value (Type conversion allowed) |
=== | 5 === "5" | Equal value & type (Strict comparison) |
!= | 5 != 3 | Not equal |
!== | 5 !== "5" | Not equal (Strict comparison) |
> | 10 > 5 | Greater than |
< | 10 < 5 | Less than |
>= | 10 >= 5 | Greater than or equal to |
<= | 10 <= 5 | Less than or equal to |
🔹 Example:
console.log(10 > 5); // Output: true
console.log(5 == "5"); // Output: true (Loose comparison)
console.log(5 === "5"); // Output: false (Strict comparison)
5️⃣ Logical Operators ⚡
Used for logical operations.
Operator | Example | Description |
---|---|---|
&& | true && false | Logical AND (Both must be true) |
` | ` | |
! | !true | Logical NOT (Reverses condition) |
🔹 Example:
console.log(true && false); // Output: false
console.log(true || false); // Output: true
console.log(!true); // Output: false
6️⃣ Bitwise Operators 🔢
Work on binary numbers.
Operator | Example | Description |
---|---|---|
& | 5 & 1 | AND |
` | ` | `5 |
^ | 5 ^ 1 | XOR |
~ | ~5 | NOT |
<< | 5 << 1 | Left shift |
>> | 5 >> 1 | Right shift |
🔹 Example:
console.log(5 & 1); // Output: 1 (0101 & 0001 = 0001)
console.log(5 | 1); // Output: 5 (0101 | 0001 = 0101)
7️⃣ Ternary Operator (Shorter If-Else) 🔥
condition ? trueValue : falseValue
🔹 Example:
let age = 18;
let status = (age >= 18) ? "Adult" : "Minor";
console.log(status); // Output: Adult
8️⃣ Type Operators 🛠️
Used to check types of variables.
Operator | Example | Description |
---|---|---|
typeof | typeof 42 | Returns "number" |
instanceof | obj instanceof Object | Checks object type |
🔹 Example:
console.log(typeof "Hello"); // Output: string
console.log(typeof 42); // Output: number
let arr = [1, 2, 3];
console.log(arr instanceof Array); // Output: true
🚀 Summary
✔️ Arithmetic Operators (+
, -
, *
, /
, %
, **
) perform calculations.
✔️ Assignment Operators (=
, +=
, -=
, etc.) assign values.
✔️ Comparison Operators (==
, ===
, !=
, >
, <
, etc.) compare values.
✔️ Logical Operators (&&
, ||
, !
) work with conditions.
✔️ Bitwise Operators (&
, |
, ^
, ~
, <<
, >>
) manipulate binary numbers.
✔️ Ternary Operator (condition ? true : false
) is a shorter if-else.
✔️ Type Operators (typeof
, instanceof
) check types of variables.