Destructuring
Destructuring lets you pull values out of arrays and objects into separate variables in one line.
Array destructuring works by position; object destructuring works by property name.
const [first, second] = [10, 20];
const { name, age } = { name: "Ada", age: 36 };
const { city = "Unknown" } = {};
- Array items match by their order
- Object values match by key name
- You can set defaults for missing values
Destructuring is common in function parameters to grab just the fields you need.
Try it yourself
Loading editor…