SyntaxStudy
Sign Up
JavaScript Beginner 4 min read

Rest and Spread

Rest and Spread

The spread operator ... expands iterables. The rest parameter collects remaining arguments into an array.

Example
function sum(...nums) { return nums.reduce((a, b) => a + b, 0); }
sum(1, 2, 3, 4); // 10
const arr1 = [1, 2]; const arr2 = [3, 4];
const merged = [...arr1, ...arr2]; // [1,2,3,4]
const obj = { a: 1 }; const clone = { ...obj, b: 2 }; // { a:1, b:2 }
Math.max(...[5, 3, 9]); // 9
Pro Tip

Spread creates a shallow copy — nested objects are still references.