Copy Objects
Spread creates a shallow copy of an object.
It is a clean way to clone without mutation.
JavaScript Tutorial
The object spread operator lets you copy and merge objects quickly.
It is a modern alternative to Object.assign for simple cases.
Copying objects is common in state updates and configuration merges.
Spread keeps the syntax short and readable.
const copy = { ...obj }
const merged = { ...a, ...b }const user = { name: "Ava" };
const copy = { ...user };
console.log(copy);Spread clones object properties.
const a = { name: "Ava" };
const b = { role: "admin" };
const merged = { ...a, ...b };
console.log(merged);Combine multiple objects.
Spread creates a shallow copy of an object.
It is a clean way to clone without mutation.
Use spread to merge multiple objects into one.
Later properties overwrite earlier ones.
Spread is shallow, so nested objects are still shared.
Use deep clone methods when necessary.
const base = { role: "user" };
const next = { ...base, role: "admin" };
console.log(next);Later keys overwrite earlier ones.
const user = { profile: { city: "Pune" } };
const copy = { ...user };
copy.profile.city = "Delhi";
console.log(user.profile.city);Nested objects are shared in shallow copies.
Without
const copy = Object.assign({}, obj);With
const copy = { ...obj };Spread is shallow; nested objects are shared.
Be mindful of property order when merging.
Clone nested objects when you need immutability.
No, it is shallow.
Use { ...a, ...b }.
When you need deep cloning or strict immutability for nested objects.
Practice: Merge user settings with defaults using spread.
const defaults = { theme: "light", layout: "grid" };
const user = { layout: "list" };
// TODO: merge into new object
One Possible Solution
const defaults = { theme: "light", layout: "grid" };
const user = { layout: "list" };
const merged = { ...defaults, ...user };
console.log(merged);No, it creates a shallow copy.
Use { ...a, ...b }.
No, it creates a new object.
Try merging objects with overlapping keys.