SYP Start Your Path

Module 1: Operators

Arithmetic Operators

let a = 10, b = 3;

a + b;  // 13
a - b;  // 7
a * b;  // 30
a / b;  // 3.333...
a % b;  // 1  — remainder (modulo)
a ** b; // 1000 — exponentiation

let x = 5;
x++; // increment, same as x = x + 1
x--; // decrement, same as x = x - 1

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 %= 4;  // x = x % 4  → 2
x **= 3; // x = x ** 3 → 8

Comparison Operators

5 == "5";   // true  — loose equality, converts types (avoid)
5 === "5";  // false — strict equality (prefer)
5 != "5";    // false
5 !== "5";    // true  — strict inequality (prefer)

10 > 5;   // true
10 < 5;   // false
10 >= 10; // true
10 <= 9;  // false

Logical Operators

let loggedIn = true, isAdmin = false;

loggedIn && isAdmin; // false — AND
loggedIn || isAdmin; // true  — OR
!loggedIn;             // false — NOT

// Short-circuiting for defaults
let username = null;
let display = username || "Guest"; // "Guest"

// Nullish coalescing — falls back only on null/undefined, not 0 or ""
let count = 0;
count ?? 10; // 0 — kept
count || 10; // 10 — wrongly treats 0 as empty

// Optional chaining — safely reads nested properties that might not exist
let user = { profile: null };
user.profile?.name; // undefined, no error

Ternary Operator

A compact shortcut for a simple if/else:

condition ? valueIfTrue : valueIfFalse

let age = 20;
let canVote = age >= 18 ? "Yes" : "No"; // "Yes"

let status = isOnline ? "🟢 Online" : "⚪ Offline";