Full answer
The symbol ... has two meanings, and the position tells which one applies.
Rest is used where values are received. In a function definition, function total(first, ...others) collects all extra arguments into a real array named others. The rest parameter must be last, and there can be only one. It replaces the old arguments object, which is array-like, not a real array, and does not exist in arrow functions. In destructuring, const [head, ...tail] = list and const { id, ...info } = object collect the remaining items or properties.
Spread is used where values are provided. In a call, Math.max(...marks) expands an array into separate arguments. In an array literal, [...a, ...b] joins arrays. In an object literal, { ...a, ...b } merges properties.
A quick test: if the dots are on the receiving side of an assignment or in a parameter list, it is rest. If they are in an expression that produces values, it is spread.
Both create new arrays or objects, and both are shallow. Spread on arrays needs an iterable, while spread in object literals needs an object (it ignores null and undefined).
function average(label, ...scores) {
const total = scores.reduce((sum, score) => sum + score, 0);
return `${label}: ${total / scores.length}`;
}
console.log(average('Maths', 70, 80, 90));
const scores = [60, 75];
console.log(average('Physics', ...scores, 90));
const [winner, ...others] = ['Isha', 'Dev', 'Sam'];
console.log(winner, others);
const { id, ...profile } = { id: 9, name: 'Kabir', city: 'Delhi' };
console.log(id, profile);
function oldStyle() {
return Array.isArray(arguments);
}
function newStyle(...args) {
return Array.isArray(args);
}
console.log(oldStyle(1, 2), newStyle(1, 2));
console.log({ ...null, ...undefined, a: 1 });
try {
console.log([...null]);
} catch (error) {
console.log(error.name);
}Run with Node.js v24.22.1 as js-23.js; two runs gave identical output. Output: Maths: 80 Physics: 75 Isha [ 'Dev', 'Sam' ] 9 { name: 'Kabir', city: 'Delhi' } false true { a: 1 } TypeError
Key points
- Rest collects into an array or object; spread expands into separate values.
- Rest appears in parameter lists and destructuring patterns; spread in calls and literals.
- A rest parameter must be last and is a real array.
- Rest replaces the arguments object, which is missing in arrow functions.
- Both produce shallow copies.
Common mistakes
- Putting a rest parameter anywhere except the last position, which is a SyntaxError.
- Believing rest and spread are the same operator in the same position.
- Using arguments inside an arrow function.
- Spreading a huge array into a call and exceeding the argument limit.
Follow-ups
- What does arguments contain, and why is it considered legacy?
- Can a function have two rest parameters?
- How does rest in object destructuring treat inherited properties?
Tips
- Use the "receiving side versus producing side" rule; it is easy to remember and easy to explain.
- Show one example of each in a single function.
- Mention that rest gives a real array so map and filter are available.