Spread and Rest
The ... operator does two related jobs. As spread it expands an array or object; as rest it gathers many values into one.
const a = [1, 2];
const b = [...a, 3, 4];
const merged = { ...{ x: 1 }, y: 2 };
function total(...nums) { return nums.length; }
- Spread copies items into a new array or object
- Rest collects extra arguments into an array
- Useful for copying without mutating the original
Spreading creates a shallow copy, which is a quick way to clone an array or object.
Try it yourself
Loading editor…