0Pricing
JavaScript Academy · Lesson

Cloning & Merging — spread and Object.assign

Clone and merge objects/arrays using spread and Object.assign; understand shallow copy and overwrite order.

Intro

Goal: Clone and merge objects/arrays safely.

  • Spread for clone/merge
  • Object.assign
  • Overwrite order
  • Shallow copy caveat
Cloning & Merging — spread and Object.assign — illustration 1

Spread: object clone

Use {...obj} for a shallow clone. Editing the clone does not change top-level fields in the original.

// Shallow clone with object spread
const user = { name: "Ayla", level: 1 };
const clone = { ...user };

clone.level = 2;

console.log("Original:", user);
console.log("Clone:", clone);
Cloning & Merging — spread and Object.assign — illustration 2

All lessons in this course

  1. Array CRUD, Iteration; Object Basics & Access
  2. Cloning & Merging — spread and Object.assign
← Back to JavaScript Academy