Object Destructuring

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — Unpacking an Object

Imagine you have an object representing a Codémon and you want to store several of its properties in variables. Normally, you would read them one by one:

const zap = { name: "Zap", type: "electric", level: 4 }; const name = zap.name; const level = zap.level; console.log(name + " is level " + level);

Just like with arrays, destructuring lets you unpack those properties in a single line. Since objects use keys instead of positions, you write the key names inside curly braces `{}` on the left of `=`:

const zap = { name: "Zap", type: "electric", level: 4 }; const { name, level } = zap; console.log(name + " is level " + level);

You only list the properties you need — here `type` is simply ignored.

Exercise 2 — The Name Must Match

With arrays, position decides everything. With objects, only the property name matters: `{ level, name }` and `{ name, level }` give the exact same result.

Because JavaScript matches variables by key name, writing a name that doesn't exist in the object (like `lvl` instead of `level`) won't crash your code — the variable simply receives `undefined`:

const zap = { name: "Zap", level: 4 }; const { name, lvl } = zap; console.log(name); console.log(lvl);

Remember the rule: with `[a, b]` you can pick any variable names you like (matched by index), whereas with `{ a, b }` the variable names must match the object's keys.

Exercise 3 — Quiz: Which Value?

What does this code print?

const item = { name: "Potion", price: 30 }; const { price, name } = item; console.log(price);
Options
• 30
• "Potion"
• undefined
• An error

Hint : The order between the braces doesn't decide anything.

Exercise 4 — Take Two Properties

Extract `name` and `hp` from the object.

Hint : Destructuring an object uses the same kind of brackets as the object itself, and each name must match a key.

Exercise 5 — Renaming a Variable

Sometimes the key name doesn't suit you, or it clashes with an existing variable. You can rename on the spot with a colon: the key on the left, your new variable name on the right.

const zap = { name: "Zap", level: 4 }; const { name: codemonName, level: codemonLevel } = zap; console.log(codemonName + " / " + codemonLevel);

Careful with the colon

In an object, `name: "Zap"` creates a property. In a destructuring, `name: codemonName` renames a variable. Same symbol, opposite direction.

Exercise 6 — Quiz: Renamed

What does this code print?

const item = { name: "Elixir", price: 120 }; const { price: cost } = item; console.log(cost);
Options
• 120
• "price"
• undefined
• An error

Hint : One side names the property to read, the other names the variable you create.

Exercise 7 — Destructuring Nested Objects

When a property holds another object, you can go one level deeper by opening braces again inside the pattern:

const trainer = { name: "Sam", starter: { name: "Aquachu", level: 5 } }; const { starter: { name: starterName, level } } = trainer; console.log(starterName + " is level " + level);

Note that `starter` itself never becomes a variable here: it's only the path to what's inside. Nesting gets unreadable fast, so use it for one level and stop.

Exercise 8 — Rename the Level

Extract `level` into a variable named `lvl`.

Hint : Renaming uses one specific symbol, with the key first and your chosen name after it.

Exercise 9 — Object Destructuring Cheat Sheet

`const o = { a: 1, b: 2 };`. Match each line with what it produces.

Items to match
• const { a } = o
• const { b: x } = o
• const { c } = o
• const { b, a } = o
Definitions
• a is 1
• x is 2
• c is undefined
• Both, order doesn't matter

Exercise 10 — Trainer Card

Build a trainer card by unpacking the data, without ever writing a dot after `trainer`.

Step 1: destructure `name` and `town` from `trainer`, renaming `town` to `city`. Step 2: in the same statement or another one, reach into `starter` to get its `name` as `starterName` and its `level`. Step 3: print the three lines shown below.

Expected output:

Trainer: Sam City: Codeville Starter: Aquachu (level 5)
Starter code
const trainer = { name: "Sam", town: "Codeville", starter: { name: "Aquachu", level: 5 } }; // Write your code here
Back to the course