Subtraction, Multiplication, and Division Assignment
What you'll learn: How to use -=, *=, and /= to update variables with arithmetic in one clean step.
The Core Idea
Just like += combines addition and assignment, JavaScript offers shorthand operators for subtraction, multiplication, and division. Instead of writing x = x - 5, you can write x -= 5. These operators modify the variable's value in place and save you from repeating the variable name.
The Three Operators
Subtraction Assignment (-=)
Subtracts a value from the variable and stores the result back:
let score = 100;
score -= 15; // Same as: score = score - 15
console.log(score); // 85
Multiplication Assignment (*=)
Multiplies the variable by a value:
let price = 20;
price *= 3; // Same as: price = price * 3
console.log(price); // 60
Division Assignment (/=)
Divides the variable by a value:
let total = 100;
total /= 4; // Same as: total = total / 4
console.log(total); // 25
Why Use Them?
Think of these operators as shortcuts that make your intent clearer. When you write inventory -= sold, you're emphasizing "reduce inventory by the sold amount" rather than the mechanics of reassignment.
They're especially useful in loops, calculations, and any time you're updating a running total or accumulator.
let balance = 500;
balance -= 50; // Subtract a fee
balance *= 1.05; // Add 5% interest
balance /= 2; // Split with a partner
console.log(balance); // 236.25
Key Takeaway: Use -=, *=, and /= to subtract, multiply, or divide a variable by a value in one concise operation, making your code cleaner and your mathematical intent more obvious.