Spread Operator (...) and Rest Parameters (...)
Spread Operator (...): 🧠Main idea: Spread expands an iterable (like an array or object) into individual elements.
Example 1: Expanding an array
Example 2: Copying an array
copy is now a new array with the same elements.
Example 3: Merging arrays
Example 4: Expanding objects
Rest Parameters (...): 🧠Main idea: Rest collects a bunch of values into a single array.
Example 1: Collecting function arguments
numbers is an array: [1, 2, 3, 4].
Example 2: Destructuring with rest
Visual Summary:
Operation | Purpose | Behavior |
---|---|---|
Spread | Expand | Breaks items out |
Rest | Collect | Gathers items together |
Simple way to remember: "Spread = break it apart. Rest = gather it together."
In short: Spread (...): Explodes values out into individual items. Rest (...): Collects multiple items into one array.