Unpack values from objects and arrays into variables, rename and default them, and build new objects with ...spread.
JavaScript · beginner · 6 min · 8 steps · free
const user = { name: 'Ada', age: 36, city: 'London' };
const { name, age } = user;
const { city: home } = user;
const [first, second = 'none'] = ['tea'];
const older = { ...user, age: 37 };
const merged = [...[1, 2], ...[3, 4]];
console.log(name, age, home, first, second);
console.log(user.age, older.age, merged);user is created and points to a new Object: {name: "Ada", age: 36, city: "London"}.const { name, age } = user takes the matching properties into two variables.{ city: home } takes the city property but names the variable home.second takes its default.{ ...user, age: 37 } copies user's properties into a new object, then overrides age.merged is created and points to a new Array: [1, 2, 3, 4].Ada 36 London tea none.Answer: undefined
Answer: 2. The later one wins.
← Objects · Classes and Inheritance →