JavaScript's ... operator performs two distinct but related tasks depending on context: Spread expands iterables into individual elements, while Rest collects multiple elements into a single array.
1. Spread Syntax (...)
Spread allows an iterable (like an array or string) to be expanded in places where zero or more arguments or elements are expected.
Copying and Merging Arrays
Spread provides a concise way to create shallow copies of arrays or merge them.
Object Literals
Spread properties can be used to shallow copy or merge objects. Properties later in the spread overwrite earlier ones with the same key.
Note: Spread performs a shallow copy. Nested objects are still referenced, not duplicated.
Function Calls
Spread can pass elements of an array as arguments to a function.
2. Rest Parameters (...)
Rest syntax looks the same but appears in function definitions or destructuring assignments. It collects the "rest" of the elements into an array.
Rest in Destructuring
You can extract specific items and collect the rest.