Spread vs Rest Operator in JavaScript

Both use the same "..." syntax, but spread expands an iterable into individual elements, while rest collects multiple elements back into a single array or ob...

Published November 21, 2026

The three dots (...) do opposite jobs depending on position: spread expands an array or object into individual items, while rest collects remaining items into an array or object.

The concept

Spread appears where values are used: function calls, array and object literals. Rest appears where values are received: function parameters and destructuring patterns.

Both create shallow copies; nested objects are still shared.

Expand vs collect

spread: [...a, ...b] → items spread out of a and b
rest: (first, ...others) → the remaining args gathered

Practical examples

Spread

const a = [1, 2], b = [3]
const all = [...a, ...b]            // [1, 2, 3]
const copy = { ...user, age: 37 }   // clone + override
Math.max(...[4, 9, 2])              // 9

Rest in parameters

const sum = (...nums) => nums.reduce((t, n) => t + n, 0)
sum(1, 2, 3) // 6

Rest in destructuring

const [first, ...others] = [1, 2, 3]   // others = [2, 3]
const { id, ...rest } = user           // rest = everything except id

Common mistakes

  • Assuming spread makes a deep copy.
  • Putting rest anywhere but last.
  • Spreading huge arrays into function arguments (stack limits).

Key takeaways

  • Spread expands, rest collects.
  • Both are shallow.
  • Rest must be last.

FAQ

Spread on objects order?

Later properties override earlier ones.

Can I spread a string?

Yes, into characters: [...'abc'].

More JavaScript articles