Episode 9 of 46
Math Operator Shorthand Methods
Learn shorthand operators for common math operations: +=, -=, ++, --, and more.
JavaScript provides shorthand operators to make common mathematical operations more concise.
Assignment Shorthand
let x = 10;
x += 5; // Same as: x = x + 5 → 15
x -= 3; // Same as: x = x - 3 → 12
x *= 2; // Same as: x = x * 2 → 24
x /= 4; // Same as: x = x / 4 → 6
x %= 4; // Same as: x = x % 4 → 2 (remainder)
x **= 3; // Same as: x = x ** 3 → 8 (exponent)
Increment and Decrement
let count = 0;
count++; // Same as: count = count + 1 → 1
count++; // → 2
count--; // Same as: count = count - 1 → 1
Prefix vs Postfix
let a = 5;
// Postfix — returns the value BEFORE incrementing
console.log(a++); // Logs 5, then a becomes 6
// Prefix — increments FIRST, then returns the value
console.log(++a); // a becomes 7, then logs 7
String Concatenation Shorthand
let greeting = "Hello";
greeting += ", World!";
console.log(greeting); // "Hello, World!"
Practical Example
let score = 0;
// Player collects coins
score += 10; // 10
score += 25; // 35
score += 10; // 45
// Player loses a life
score -= 15; // 30
console.log("Final score:", score);