Spread Operator for Objects
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Spreading an Object
Imagine you have a Codémon object and you want to create a copy of it — or a new version with extra properties — without modifying the original. The spread operator (`...`) works inside curly braces `{}` to copy every `key: value` pair into a new object:
const zap = { name: "Zap", level: 4 };
const copy = { ...zap };
console.log(copy.name + " Lv." + copy.level);You can also add brand-new properties in the very same object, separated by commas:
const zap = { name: "Zap", level: 4 };
const full = { ...zap, type: "electric" };
console.log(full.name + " | " + full.type + " | Lv." + full.level);Exercise 2 — The Last One Wins
What happens if you spread an object and also write a property with the same key? In JavaScript objects, when a key appears more than once, the last one written always wins. This makes it super easy to create an updated copy of an object:
const zap = { name: "Zap", level: 4 };
const levelledUp = { ...zap, level: 5 };
console.log(levelledUp.name + " is now level " + levelledUp.level);Because the last key wins, order matters! Always put `...zap` first and your updated properties after it. If you put `...zap` at the end, its old values will overwrite your changes:
const zap = { name: "Zap", level: 4 };
const wrong = { level: 5, ...zap };
console.log(wrong.level);Exercise 3 — Quiz: Which Value Survives?
What does this code print?
const item = { name: "Potion", price: 30 };
const sale = { ...item, price: 15 };
console.log(sale.price);153045undefinedHint : Read the braces from left to right and see which value is written last.
Exercise 4 — Modified Copy
Build a copy of `zap` where the level is 10.
Hint : One symbol pours in the existing properties, and a property is written with its usual separator.
Exercise 5 — Merging Two Objects
Two spreads in a row merge two objects. Useful when one object holds defaults and the other holds the values the player actually chose:
const defaults = { type: "normal", level: 1, hp: 20 };
const chosen = { name: "Metachu", type: "metal", level: 15 };
const cdm = { ...defaults, ...chosen };
console.log(cdm.name + " | " + cdm.type + " | Lv." + cdm.level + " | " + cdm.hp + " HP");The chosen values win because they come last, and `hp` survives from the defaults because `chosen` doesn't mention it.
Exercise 6 — Quiz: Merged
What does this code print?
const a = { x: 1, y: 2 };
const b = { y: 9 };
const c = { ...a, ...b };
console.log(c.x + " " + c.y);1 91 29 2undefined 9Hint : One key exists in both objects, the other in only one.
Exercise 7 — Updating Without Modifying
This pattern is everywhere in modern JavaScript: instead of changing an object, you build a new one from it. The original stays exactly as it was.
const zap = { name: "Zap", hp: 45 };
const damaged = { ...zap, hp: 20 };
console.log("Original: " + zap.hp);
console.log("New: " + damaged.hp);Why bother?
Keeping the original intact makes bugs much easier to track: nothing changes behind your back. It's also how most modern frameworks expect you to handle data.
Exercise 8 — Apply the Damage
Build `hurt`, a copy of `pyrochu` with 10 HP, without touching the original.
Hint : Pour in everything first, then write the one property you want to change.
Exercise 9 — Object Spread Cheat Sheet
`const o = { a: 1, b: 2 };`. Match each line with its result.
{ ...o }{ ...o, c: 3 }{ ...o, a: 9 }{ a: 9, ...o }Exercise 10 — Level Up Without Damage
Zap wins a battle. Produce the updated Codémon as a new object, never modifying the original.
Step 1: build `complete` by merging `defaults` and `zap`, so the missing properties are filled in. Step 2: build `afterBattle` from `complete`, with the level increased by 1 and a new property `wins` worth 1. Step 3: print the two lines shown below, the first from `afterBattle` and the second from the original `zap`.
Expected output:
Zap | electric | Lv.5 | 1 win(s)
Original level: 4const defaults = { type: "normal", level: 1 };
const zap = { name: "Zap", type: "electric", level: 4 };
// Write your code here