BeginnerCoder

Lesson 6 — Merging with spread

The ... operator

Three dots do two opposite jobs. Spread opens an array (or object) up, so its items are copied into a new one. Rest does the reverse in a function's parameters: it collects the remaining arguments into an array.

const nums = [1, 2];
const more = [0, ...nums, 3];      // [0, 1, 2, 3]
const sum = (...all) => all.length; // rest: all is an array
console.log(sum(5, 6, 7));          // 3

Spread makes a new array — the originals stay unchanged.

Why it matters: copying and merging without changing the original is how React and most modern code update data.

Want it another way?
Task

Fill all by spreading a and b into one new array. a must stay [ 1, 2 ].

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.