Assignment
Operators
Master the shorthand operators that make updating variables in JavaScript faster, cleaner, and less error-prone.
What Is Assignment?
In JavaScript, assignment means storing a value in a variable. The most basic assignment operator is the equals sign =. It takes the value on the right and places it into the variable on the left.
score = 10; // score now holds 10
The Problem: Repetition
When you want to update a variable based on its current value, you end up writing the variable name twice:
lives = lives - 1; // subtract 1 from lives
price = price * 0.9; // apply 10% discount
The Solution: Compound Assignment
Compound assignment operators combine an arithmetic operation with assignment into a single step. They read the current value, perform the operation, and store the result: all in one expression.
lives -= 1; // same as lives = lives - 1
price *= 0.9; // same as price = price * 0.9
The Core Rule
The Five Operators You Need
| Operator | Shorthand | Equivalent | Purpose |
|---|---|---|---|
+= |
x += 3 |
x = x + 3 |
Addition |
-= |
x -= 3 |
x = x - 3 |
Subtraction |
*= |
x *= 3 |
x = x * 3 |
Multiplication |
/= |
x /= 3 |
x = x / 3 |
Division |
%= |
x %= 3 |
x = x % 3 |
Remainder |
= vs +=: Key Distinction
x = 5; // x is now 5 (10 is gone)
let y = 10;
y += 5; // y is now 15 (10 + 5)
How to Apply Them
x = x op value anywhere the same variable appears on both sides.+ becomes +=, - becomes -=, and so on.=.Example 1: Score Counter
A game starts at 0 points. The player earns 25 points, then 10 more, then loses 5.
score += 25; // 0 + 25 = 25
score += 10; // 25 + 10 = 35
score -= 5; // 35 - 5 = 30
console.log(score); // 30
Example 2: Discount Calculator
A product costs $200. Apply a 15% discount, then add $12 shipping.
price *= 0.85; // 200 * 0.85 = 170
price += 12; // 170 + 12 = 182
console.log(price); // 182
Example 3: Remainder in Action
The remainder operator %= is useful for wrapping values. Imagine a clock-style counter that cycles from 0 to 3:
counter += 1; // 1
counter += 1; // 2
counter += 1; // 3
counter += 1; // 4
counter %= 4; // 4 % 4 = 0 (wraps back!)
console.log(counter); // 0
+= with Strings
The += operator also works with strings: it concatenates (joins) text onto the existing value.
greeting += " "; // "Hello "
greeting += "World"; // "Hello World"
console.log(greeting); // "Hello World"
How Values Flow
Common Mistakes
=+ instead of +=. The expression x =+ 5 means "assign positive 5" (like x = +5), not addition.constconst cannot be reassigned. Using += on a const throws a TypeError.+= with a number and a string concatenates instead of adding: 5 += "2" gives "52", not 7.Summary Table
| Operator | Example | Long Form | Result (x=10) |
|---|---|---|---|
+= |
x += 3 |
x = x + 3 |
13 |
-= |
x -= 3 |
x = x - 3 |
7 |
*= |
x *= 3 |
x = x * 3 |
30 |
/= |
x /= 4 |
x = x / 4 |
2.5 |
%= |
x %= 3 |
x = x % 3 |
1 |
Question 1 of 5
What is the value of x after this code runs?let x = 8; x += 4;
x += 4 means x = x + 4, so 8 + 4 = 12.Question 2 of 5
Which of the following is equivalent to total = total * 3?
*= means "multiply and assign." =* is not valid JavaScript syntax.Question 3 of 5
What does count %= 5 do when count is 13?
%= computes the remainder. 13 % 5 = 3 because 5 goes into 13 twice (10) with 3 left over.Question 4 of 5
What is the final value of n?let n = 20; n -= 8; n /= 3;
Question 5 of 5
What happens when you run let msg = "Hi"; msg += " there";?
+= operator works with strings too: it concatenates (joins) the right side onto the left side. "Hi" + " there" = "Hi there".