Skip to content
Arrays & ObjectsChapter 24 / 47Beginner5 min read

Spread & Rest

Learn the difference between the three dots in JavaScript spread and rest, how to copy and merge arrays and objects, and how rest parameters work.

On this page

1. What are Spread & Rest?

The syntax is exactly the same, only the position differs. Where a value is expected (the right side, a function call), ... is spread. Where a variable or parameter is expected (the left side, a function definition), ... is rest.

2. Why does it matter?

In older code, you used concat to merge arrays, Object.assign for objects, and the arguments object for arguments. Spread and rest do all of this with one clean syntax.

The old waySpread and rest
a.concat(b)Spread both inside an array literal
Object.assign with an empty objectSpread inside an object literal
Math.max.apply(null, arr)Spread the array into Math.max
The arguments object, not a real arrayA rest parameter, a real array

3. Where is it used?

SituationSpread or rest
React state update (a new array / object)Spread
Merging default settings with user settingsSpread
A shallow copy of an arraySpread
A function that takes any number of argumentsRest
Removing one field from an object and keeping the restRest (destructuring)
Splitting a string into charactersSpread

4. How does it work?

PositionWhat it isExample
In an array literalSpread[...a, ...b]
In an object literalSpread{ ...defaults, ...user }
In a function callSpreadMath.max(...nums)
In function parametersRestfunction sum(...nums)
In destructuringRestconst { id, ...rest } = obj
Spread vs rest
One array or object
Spread: take the values out and spread them
Many separate values
Rest: collect the leftover values into one array or object
One array or object

In object spread, the later property wins. So in { ...defaults, ...user }, the user's values override the defaults.

5. Code example

Spread

js
const fruits = ["apple", "mango"];
const more = [...fruits, "banana"];

const defaults = { theme: "light", fontSize: 16, lang: "en" };
const saved = { theme: "dark" };
const settings = { ...defaults, ...saved, lang: "ur" };

const scores = [40, 90, 75];

console.log(more);
console.log(settings);
console.log(Math.max(...scores));
console.log([..."Hi!"]);

Rest

js
function sum(label, ...nums) {
  const total = nums.reduce((acc, n) => acc + n, 0);
  return `${label}: ${total}`;
}

console.log(sum("Total", 10, 20, 30));

const user = { id: 1, name: "Ali", password: "secret" };
const { password, ...safeUser } = user;
console.log(safeUser);

const [winner, ...others] = ["Sara", "Ali", "Hina"];
console.log(winner, others);

How does the code work?

PartMeaning
[...fruits, "banana"]A new array, fruits stays the same
{ ...defaults, ...saved, lang: "ur" }Merge, later values win: dark, 16, ur
Math.max(...scores)Turned the array into separate arguments, 90
[..."Hi!"]["H", "i", "!"]
...nums10, 20, 30 in one array, total 60
{ password, ...safeUser }password set aside, the rest is { id, name }
[winner, ...others]Sara and ["Ali", "Hina"]

Interview tip

Spread makes a shallow copy. Nested objects still share the same reference. For a deep copy, use structuredClone. Also, a rest parameter must always be the last parameter.

6. Common mistakes

Mistake: Thinking spread is a deep copy

Why? Nested objects and arrays are not copied, so changing the copy also changes the original's nested data.

Better: Use structuredClone for nested data, or spread at every level.

Mistake: The wrong order in a merge

Why? In { ...user, ...defaults }, the defaults overwrote the user's values.

Better: Write the value that should win later: { ...defaults, ...user }.

Mistake: Putting a rest parameter in the middle

Why? function f(...args, last) throws a SyntaxError.

Better: Rest must always be the last parameter: function f(first, ...others).

Mistake: Spreading very large arrays into a function call

Why? Hundreds of thousands of items in Math.max(...arr) can exceed the argument limit and throw a RangeError.

Better: For very large lists, use reduce or a simple loop.

7. What to learn before and after

Suggested order
Destructuring
Spread & Rest
Optional Chaining & Nullish
Map & Set

Final quick revision

PointDetail
SpreadExpands values (array, object, function call)
RestGathers leftover values (parameters, destructuring)
Object mergeThe later property wins
CopyShallow, nested data stays shared
Rest positionAlways last