Skip to content
techsnow

Arrays & Objects

Destructuring and Spread

Destructuring: unpack values

const user = { name: "Sara", city: "Lahore", age: 22 };
const { name, city } = user;

const colours = ["red", "green", "blue"];
const [first, second] = colours;

console.log(name, city, first, second);

Output

Sara Lahore red green

Spread: copy and merge

const a = [1, 2];
const b = [...a, 3, 4];           // [1, 2, 3, 4]

const settings = { theme: "light", lang: "en" };
const updated = { ...settings, theme: "dark" }; // { theme: "dark", lang: "en" }