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

Adding Elements with Spread

Insert elements at the beginning, middle, or end of arrays using spread syntax.

Adding Elements with Spread

You'll learn how to insert new elements anywhere in an array without mutating the original—using nothing but spread syntax.

The Core Idea

You already know how to copy and concatenate arrays with spread. Now it's time to level up: you can position new elements precisely by spreading before or after them, or even split an existing array to insert elements in the middle.

Think of spread like unzipping a backpack and laying out its contents. You can place new items at the front, back, or even in the middle of the line before zipping everything back up into a new bag.

Adding to the Beginning

const colors = ['green', 'blue'];
const updated = ['red', ...colors];
// ['red', 'green', 'blue']

The new element goes first, then the spread unpacks the rest.

Adding to the End

const colors = ['red', 'green'];
const updated = [...colors, 'blue'];
// ['red', 'green', 'blue']

Spread first, new element last.

Adding to the Middle

const colors = ['red', 'blue'];
const updated = [colors[0], 'green', ...colors.slice(1)];
// ['red', 'green', 'blue']

// Or with two spreads:
const list = ['a', 'b', 'e'];
const withMiddle = [...list.slice(0, 2), 'c', 'd', ...list.slice(2)];
// ['a', 'b', 'c', 'd', 'e']

Slice the array at your insertion point, spread both halves, and sandwich your new elements in between.

Why This Matters

Every example creates a new array—the original remains untouched. This immutability keeps your data predictable and prevents bugs in larger applications.

Key Takeaway: Use spread to insert elements at any position—beginning (['new', ...arr]), end ([...arr, 'new']), or middle (split with .slice() and spread both parts)—while keeping your original array intact.