JavaScript Operators Tutorial
📌 Introduction
Operators in JavaScript are special symbols or keywords that perform operations on values and variables. They help us do calculations, comparisons, logic checks, and more. Think of operators as the tools 🧰 you use to work with data.
🧠 Types of JavaScript Operators
JavaScript operators can be grouped into several categories:
- ➕ Arithmetic Operators
- ⚖️ Comparison Operators
- 🧩 Logical Operators
- 📌 Assignment Operators
- 🔢 Bitwise Operators
- ❓ Other Operators (typeof, ternary, etc.)
➕ Arithmetic Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Addition | 5 + 2 | 7 |
| - | Subtraction | 5 - 2 | 3 |
| * | Multiplication | 5 * 2 | 10 |
| / | Division | 5 / 2 | 2.5 |
| % | Modulus (Remainder) | 5 % 2 | 1 |
| ** | Exponentiation | 5 ** 2 | 25 |
Arithmetic Operators Example
let a = 10, b = 3;
console.log(a + b); // 13
console.log(a % b); // 1
console.log(a ** b); // 1000⚖️ Comparison Operators
Comparison operators compare two values and return true or false.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal (loose) | 5 == "5" | true |
| === | Strict equal | 5 === "5" | false |
| != | Not equal | 5 != 4 | true |
| !== | Strict not equal | 5 !== "5" | true |
| > | Greater than | 7 > 5 | true |
| < | Less than | 7 < 5 | false |
🧩 Logical Operators
Logical operators combine conditions.
- && ➝ Logical AND (both must be true).
- || ➝ Logical OR (at least one true).
- ! ➝ Logical NOT (reverses condition).
Logical Operators Example
let x = true, y = false;
console.log(x && y); // false
console.log(x || y); // true
console.log(!x); // false📌 Assignment Operators
Assignment operators are used to assign values to variables.
| Operator | Meaning | Example |
|---|---|---|
| = | Assign | x = 10 |
| += | Add and assign | x += 5 |
| -= | Subtract and assign | x -= 3 |
| *= | Multiply and assign | x *= 2 |
🔢 Bitwise Operators
Bitwise operators work on binary numbers at the bit level.
- & ➝ AND
- | ➝ OR
- ^ ➝ XOR
- ~ ➝ NOT
- << ➝ Left Shift
- >> ➝ Right Shift
❓ Other Useful Operators
- typeof ➝ Returns type of a variable.
- instanceof ➝ Checks if an object belongs to a class.
- ?: ➝ Ternary operator (short if-else).
- delete ➝ Deletes object property.
- in ➝ Checks if property exists in an object.
Other Operators Example
let age = 20;
console.log(typeof age); // "number"
console.log(age >= 18 ? "Adult" : "Minor"); // "Adult"Note
💡 Tip: Use === (strict equality) instead of == to avoid unexpected type coercion in JavaScript.
>>"Operators are the language of logic in programming – master them, and you master the flow of code." 🧠