Operators are symbols that perform operations on values and variables. Think of them as the "verbs" of your code.
1. Arithmetic Operators
Used for math.
+(Addition),-(Subtraction),*(Multiplication),/(Division)%(Remainder/Modulo): Returns what's left over after division (e.g.,10 % 3is1).**(Exponentiation): Raising to a power (e.g.,2 ** 3is8).
2. Assignment Operators
Used to assign values to variables.
=: Basic assignment.+=,-=: Add or subtract and then assign (e.g.,x += 5is the same asx = x + 5).
3. Comparison Operators
Used to compare two values. They always return a Boolean (true or false).
==vs===: Always use===(Strict Equality). It checks both the value and the data type.!=vs!==: Not equal.>,<,>=,<=: Greater than, less than, etc.
4. Logical Operators
Used to combine multiple conditions.
&&(AND): True if both sides are true.||(OR): True if at least one side is true.!(NOT): Flips the value (true becomes false).
The Code Example
// 1. Math in Action
let score = 10 + 5 * 2; // Follows PEMDAS (10 + 10 = 20)
let remainder = 10 % 3; // Result: 1 (3 goes into 10 thrice, 1 left over)
// 2. Short-hand assignment
let lives = 3;
lives -= 1; // Now lives is 2
// 3. Comparison (The Triple Equals Rule)
console.log(5 == "5"); // true (Weak comparison, ignores type)
console.log(5 === "5"); // false (Strict comparison, Number !== String)
// 4. Logic Gates
let hasCoffee = true;
let isTired = true;
let canWork = hasCoffee && isTired; // true (Both are true)
let isWeekend = false;
let isHoliday = true;
let stayHome = isWeekend || isHoliday; // true (One is true)