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.

Lesson 2 of 28 steps

Cloning & Merging — spread and Object.assign is a free JavaScript Academy lesson on CoddyKit. This is lesson 2 of 2. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 2 lessons in total.

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

Object.assign: clone

Object.assign({}, obj) also makes a shallow clone. Avoid mutating the source by assigning into an empty object.

// Shallow clone with Object.assign
const settings = { theme: "dark", compact: false };
const copy = Object.assign({}, settings);

copy.compact = true;

console.log("Settings:", settings);
console.log("Copy:", copy);
Cloning & Merging — spread and Object.assign — illustration 3

Merge: order matters

Merging is order-sensitive: later sources overwrite earlier keys (b becomes 9 here).

// Merge: later keys overwrite earlier ones
const base = { a: 1, b: 2 };
const extra = { b: 9, c: 3 };

const mergedSpread = { ...base, ...extra };
const mergedAssign = Object.assign({}, base, extra);

console.log("Spread merged:", mergedSpread);
console.log("Assign merged:", mergedAssign);
Cloning & Merging — spread and Object.assign — illustration 4

Array spread: clone & merge

[...arr] clones an array. Combine arrays with [...a, ...b] without mutating.

// Arrays: clone and merge with spread
const nums = [1, 2, 3];
const cloneNums = [...nums];

const more = [4, 5];
const mergedNums = [...nums, ...more];

console.log("nums:", nums);
console.log("clone:", cloneNums);
console.log("merged:", mergedNums);
Cloning & Merging — spread and Object.assign — illustration 5

Shallow vs deep

Shallow copy: nested objects/arrays are shared. For deep structures, copy inner levels explicitly.

// Shallow copy caveat: nested objects are shared
const original = { user: { name: "Mina" }, active: true };
const shallow = { ...original };

// Mutate nested object
shallow.user.name = "Lena";

console.log("Original:", original);
console.log("Shallow:", shallow);
Cloning & Merging — spread and Object.assign — illustration 6

Clone method quiz

Quick check: Shallow clone without mutation.

Cloning & Merging — spread and Object.assign — illustration 7

Recap

Recap: Clone with spread or Object.assign into an empty object, merge by placing later sources last, and remember: these are shallow copies.

Cloning & Merging — spread and Object.assign — illustration 8
Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

Is the “Cloning & Merging — spread and Object.assign” lesson free?

Yes — any 3 lessons of the JavaScript Academy learning path, including “Cloning & Merging — spread and Object.assign”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 2 lessons in total.

What will I learn in “Cloning & Merging — spread and Object.assign”?

Clone and merge objects/arrays using spread and Object.assign; understand shallow copy and overwrite order. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 2.

How long does the “Cloning & Merging — spread and Object.assign” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

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