<htmlstart/>

JS Operators

Arithmetic, comparison, logical, and assignment operators.

Arithmetic Operators

5 + 2   // 7   addition
5 - 2   // 3   subtraction
5 * 2   // 10  multiplication
5 / 2   // 2.5 division
5 % 2   // 1   remainder (modulo)
5 ** 2  // 25  exponentiation
5 + 2;  // always use ; at end of statements

Assignment Operators

let x = 10;
x += 5;   // x = x + 5  → 15
x -= 3;   // x = x - 3  → 12
x *= 2;   // x = x * 2  → 24
x /= 4;   // x = x / 4  → 6
x **= 2;  // x = x ** 2 → 36
x++;      // x = x + 1  → 37
x--;      // x = x - 1  → 36

Comparison Operators

5 === 5    // true  — strict equal (value + type)
5 !== "5"  // true  — strict not equal
5 == "5"   // true  — loose equal (avoid)
5 > 3      // true
5 < 3      // false
5 >= 5     // true
5 <= 4     // false

Logical Operators

true && false   // false  — AND (both must be true)
true || false   // true   — OR  (at least one must be true)
!true           // false  — NOT (inverts boolean)

// Nullish coalescing — use right side if left is null/undefined
const name = user.name ?? "Guest";

// Optional chaining — safe property access
const city = user?.address?.city;  // undefined instead of error

Ternary Operator

// condition ? valueIfTrue : valueIfFalse
const age = 20;
const label = age >= 18 ? "Adult" : "Minor";  // "Adult"

// Nested (keep shallow)
const grade = score >= 90 ? "A" : score >= 70 ? "B" : "C";
Always use === and !== for comparisons. The loose == operator applies type coercion rules that surprise even experienced developers.
🧩

Test Yourself

4 questions
Q1.What does the % operator return?
Q2.What does the ?? operator do?
Q3.What does x += 5 mean?
Q4.What is the result of the ternary expression: 10 > 5 ? "yes" : "no"?