Course contentsShow
← JavaScript
Lesson 961 of 2,72322. Spread and RestFree lesson

Copying Arrays with Spread

Create shallow copies of arrays using spread syntax instead of slice or concat.

Copying Arrays with Spread

You'll learn how to create shallow copies of arrays using the spread operator instead of older methods like slice() or concat().

The Core Idea

The spread operator (...) makes it incredibly easy to duplicate an array without affecting the original. When you spread an array into a new array literal, JavaScript creates a fresh copy with all the same elements. This is what we call a shallow copy—the new array is independent, but if elements are objects, they still reference the same objects.

Think of it like photocopying a list of addresses. You get a new piece of paper (the array), but the addresses themselves still point to the same houses.

Creating Copies with Spread

const original = [1, 2, 3, 4];
const copy = [...original];

copy.push(5);

console.log(original); // [1, 2, 3, 4] — unchanged!
console.log(copy);     // [1, 2, 3, 4, 5]

Compare this to older approaches:

// Old way with slice()
const copySlice = original.slice();

// Old way with concat()
const copyConcat = [].concat(original);

// Modern way with spread (cleaner!)
const copySpread = [...original];

Why Spread is Better

The spread syntax is more readable and expressive. It clearly shows your intent: "I'm creating a new array with these elements." It also combines naturally with adding extra elements:

const numbers = [2, 3, 4];
const extended = [1, ...numbers, 5, 6]; // [1, 2, 3, 4, 5, 6]

Key Takeaway: Use [...array] to create shallow copies of arrays—it's cleaner and more modern than slice() or concat(), and it makes your code's intent crystal clear.