Spread and Rest with Arrays
The spread operator (...) and rest parameters were introduced in ES2015 and significantly simplified working with arrays and function arguments. They look identical syntactically but serve opposite purposes: spread expands an iterable into individual items, while rest collects individual items into an array.
Spread Operator
Place ... before an array (or any iterable) to expand its elements into a new context — a new array literal, a function call, or another spread expression. This is the cleanest way to copy or merge arrays without mutating originals.
Rest Parameters
In a function parameter list, ...args gathers all remaining arguments into a real array. Unlike the old arguments object, a rest parameter is a true array with all array methods available.
Practical Patterns
- Shallow clone:
const copy = [...original] - Merge:
const merged = [...a, ...b] - Prepend/append:
const newArr = [0, ...arr, 99]