If variables are the "nouns" of programming (the things), operators are the verbs and conjunctions. They let us do math, compare values, and make logical decisions.
Think of a login screen. How does it work?
It uses operators: "IF (password === correctPassword && 2FA === true) THEN login."
1. Arithmetic (Math)
JavaScript handles math just like a calculator.
let price = 10;
let tax = 2;
console.log(price + tax); // Addition
console.log(price - tax); // Subtraction
console.log(price * tax); // Multiplication
console.log(price / tax); // Division
// The "Modulus" operator (%) gives the REMAINDER
// Useful for checking if a number is even or odd
console.log(10 % 3); // 1 (Because 3 goes into 10 three times, with 1 left over)2. Shortcut Assignment
Programmers are lazy (efficient). Instead of writing x = x + 5, we have shortcuts.
let score = 100;
score += 10; // Same as: score = score + 10
console.log(score); // 110
score -= 50; // Same as: score = score - 50
console.log(score); // 60
score++; // Increment by 1
console.log(score); // 613. The Dangerous "Double Equals"
Professional Rule #1
NEVER use ==. ALWAYS use ===.== performs "type coercion", meaning it tries to convert types to match. This leads to horrible bugs.=== checks value AND type (Strict Equality).
const numberFive = 5;
const stringFive = "5";
// LOOSE Equality (Bad)
// JS converts the string "5" to number 5 implicitly.
console.log(numberFive == stringFive); // true (Wait, what?)
// STRICT Equality (Good)
// JS sees one is a Number and one is a String. Different.
console.log(numberFive === stringFive); // false4. Logic Gates
These allow you to combine multiple requirements.
&&(AND): Both sides must be true. (e.g., Has Ticket AND Has ID).||(OR): Only one side needs to be true. (e.g., Pay with Cash OR Pay with Card).!(NOT): Flips the value. (e.g., NOT logged in).
const isAdult = true;
const hasTicket = false;
const isVip = true;
// The Club Entry Logic
const canEnter = (isAdult && hasTicket) || isVip;
console.log("Can enter?", canEnter);
// Why true?
// (true && false) is FALSE.
// BUT (false || true) is TRUE. VIP saves the day.