The ... syntax serves two purposes: spreading iterables and collecting rest parameters. Here's how to use both effectively.
Spread Operator Basics#
Spread with Objects#
Rest Parameters#
Array Destructuring with Rest#
Object Destructuring with Rest#
Function Arguments#
Copying and Merging#
Immutable Updates#
Conditional Spread#
Common Patterns#
Converting Iterables#
Edge Cases#
Performance Considerations#
Best Practices#
Spread Arrays:
✓ Copy arrays immutably
✓ Combine arrays
✓ Convert iterables
✓ Spread into function calls
Spread Objects:
✓ Shallow copy objects
✓ Merge configurations
✓ Add/override properties
✓ Immutable updates
Rest Parameters:
✓ Variable argument functions
✓ Destructuring remainder
✓ Forwarding arguments
✓ Always last parameter
Avoid:
✗ Spread in tight loops
✗ Assuming deep copy
✗ Spreading non-iterables
✗ Overcomplicating simple operations
Conclusion#
The spread (...) and rest operators are versatile tools for working with arrays, objects, and function parameters. Use spread for copying, merging, and converting iterables. Use rest for collecting function arguments and destructuring remainders. Remember that spread creates shallow copies - nested objects still share references. These operators enable clean, functional-style JavaScript with immutable data patterns.