Destructuring
Destructuring pulls values out of arrays or objects into variables in one line. For arrays the position matters and for objects the names matter. You can rename and give defaults too.
Example
const [first, second] = ["red", "green", "blue"];
console.log(first, second);
const user = { name: "Ada", age: 21, city: "Lagos" };
const { name, city: town, role = "student" } = user;
console.log(name, town, role);
let a = 1, b = 2;
[a, b] = [b, a];
console.log(a, b);Exercise
Unpack the name property.
const { } = { name: "Ada" };
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.