Spread / Rest operator
function
ES6
Spread expands iterables in places expecting multiple elements. Rest collects remaining arguments.
Syntax
const newArr = [...arr1, ...arr2]; function fn(...args)
Example
javascript
// Spread arrays
const a = [1, 2, 3];
const b = [4, 5, 6];
const combined = [...a, ...b]; // [1,2,3,4,5,6]
// Spread objects (shallow clone)
const user = { name: 'Alice', age: 28 };
const updated = { ...user, age: 29 };
// Rest in function
function sum(...numbers) {
return numbers.reduce((a, b) => a + b, 0);
}
console.log(sum(1, 2, 3, 4)); // 10