๐ŸŒŸ JavaScript

Spread vs Rest โ€” Same Dots, Opposite Jobs

๐Ÿ“… Jul 4, 2026 โฑ 3 min read

Three dots, two meanings: spread unpacks; rest collects. Position decides which.

Spread โ€” expand into pieces

const merged  = [...arr1, ...arr2];
const copy    = [...arr];                       // shallow copy
const updated = { ...user, city: "Chennai" };   // clone + override โ€” React state!
Math.max(...numbers);
const chars  = [..."hello"];                    // ["h","e","l","l","o"]
const unique = [...new Set(arr)];               // dedupe one-liner

Rest โ€” collect the leftovers

function sum(...nums) { return nums.reduce((a,b) => a+b, 0); }
const [first, ...others] = list;
const { password, ...safeUser } = user;   // omit a field โ€” send safeUser to the client

The trap: shallow means shallow

const copy = { ...student };
copy.marks.cn = 0;          // โš ๏ธ ALSO changes student.marks.cn!
// nested objects are shared references. Deep copy:
const deep = structuredClone(student);

Interview one-liner: "spread in calls/literals unpacks; rest in parameters/destructuring gathers โ€” and both copy only one level deep." Full lesson: Spread & Rest.

โ† All Articles