Beginner · 9 min
Destructuring values
Read object properties and array items into named variables.
Unpack an object
Object destructuring selects properties by name. A default applies when the selected property is undefined.
const user = { name: "Ada" };
const { name, city = "Unknown" } = user;
console.log(name);
console.log(city);Output
Ada Unknown
Unpack an array
Array destructuring selects values by position. Use names that describe what each position means.
const coordinates = [4, 7];
const [row, column] = coordinates;
console.log(row + column);Output
11
Put it into practice
- Predict each result before running the example.
- Complete the coding task, then explain why your solution works.
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Read a product
Destructure price and quantity from product and use them to set total. Display total.
Select both properties, then multiply them.