Assignment Operators
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Introduction to Assignment Operators
You already know the basic assignment operator =, which stores a value in a variable. But what if you want to modify a variable based on its current value?
For example, when your Codémon gains experience points, you want to add to its existing XP:
let aquachuXP = 100;
console.log("Starting XP:", aquachuXP);
// Add 50 XP after winning a battle
aquachuXP = aquachuXP + 50;
console.log("After battle:", aquachuXP);This pattern of variable = variable + value is so common that JavaScript provides a shortcut: compound assignment operators.
The code above can be rewritten as:
let aquachuXP = 100;
console.log("Starting XP:", aquachuXP);
// Shorter way to add 50 XP!
aquachuXP += 50;
console.log("After battle:", aquachuXP);Both ways do exactly the same thing, but += is shorter and clearer!
Exercise 2 — All the Compound Assignment Operators
JavaScript has a compound assignment operator for each arithmetic operator. Here they all are:
let a = 10;
a += 5;
let b = 20;
b -= 8;
let c = 7;
c *= 3;
let d = 100;
d /= 4;
let e = 17;
e %= 5;
let f = 2;
f **= 3;
console.log(a, b, c, d, e, f);Why use compound operators?
Compound operators make your code shorter and easier to read. When you see health += 20, you immediately know you're adding to the existing health value. It's a common pattern that all programmers recognize!
Exercise 3 — Understanding Compound Assignment
Which of the following is equivalent to score = score * 3;?
score *= 3;score =* 3;score += 3;score = 3;Hint : Look for the operator that combines multiplication with assignment. The operator comes before the equals sign.
Exercise 4 — Converting to Compound Operators
Pyrochu just won 5 levels! Rewrite the code using a compound assignment operator:
Hint : When you see variable = variable + value, you can replace it with variable += value.
Exercise 5 — Practice with Different Operators
Complete the code to reduce Florachu's energy by 15 using a compound operator:
Hint : To reduce a value, you need the subtraction compound operator. Think about which operator subtracts.
Exercise 6 — Predicting the Result
What will be the final value of coins after running this code?
let coins = 50;
coins += 30;
coins -= 20;
console.log(coins);Hint : Follow the operations step by step. Start with 50, then add 30, then subtract 20.
Exercise 7 — Match the Operators
Match each compound operator with its long form equivalent:
x += 10x *= 2x /= 5x -= 7x %= 3x = x + 10x = x * 2x = x / 5x = x - 7x = x % 3Exercise 8 — Complex Operations
What is the value of damage after this code runs?
let damage = 10;
damage *= 3;
damage += 5;
console.log(damage);Hint : Execute each operation in order: first multiply 10 by 3, then add 5 to that result.
Exercise 9 — Real-World Application
Your Codémon's defense doubles when it uses a shield move. Complete the code using a compound operator:
Hint : To double a value means to multiply it by 2. Which compound operator performs multiplication?
Exercise 10 — Codémon Battle Stats
Your Aquachu is in an intense battle! Create a program that tracks its stats through the battle: 1. Start with: - HP: 100 - Attack: 50 - Defense: 30 - XP: 200 2. During the battle: - Aquachu takes 25 damage (reduce HP) - Uses a power-up that doubles its attack - Enemy's critical hit reduces defense by 10 - After winning, gains 150 XP 3. Print all final stats with descriptive labels. Use compound assignment operators for all modifications!
// Initialize Aquachu's stats
// Battle sequence
// Display final stats