JavaScript Spread and Rest Operators

The spread and rest operators, introduced in ECMAScript 6 (ES6), provide powerful ways to work with arrays, objects, and function parameters in JavaScript. The spread operator allows an iterable (like an array) to be expanded, while the rest operator allows multiple elements to be collected into a single array.

Spread Operator

The spread operator (...) allows an iterable (such as an array) to be expanded into individual elements. It's useful for combining arrays, copying arrays, spreading elements in function calls, and more.

Example: Combining Arrays
Try yourself
        
            const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const combinedArr = [...arr1, ...arr2];
console.log(combinedArr); // Output: [1, 2, 3, 4, 5, 6]
        
    

This example demonstrates how to combine two arrays using the spread operator.


Example: Copying Arrays
Try yourself
        
            const originalArray = [1, 2, 3];
const copiedArray = [...originalArray];
console.log(copiedArray); // Output: [1, 2, 3]
        
    

This example shows how to create a shallow copy of an array using the spread operator.


Example: Spreading Elements in Function Calls
Try yourself
        
            function sum(a, b, c) {
    return a + b + c;
}
const numbers = [1, 2, 3];
console.log(sum(...numbers)); // Output: 6
        
    

This example demonstrates how to use the spread operator to pass elements of an array as arguments to a function.


Rest Operator

The rest operator (...) allows you to represent an indefinite number of elements as an array. It's useful for handling function parameters and destructuring arrays and objects.

Example: Function Parameters
Try yourself
        
            function multiply(multiplier, ...numbers) {
    return numbers.map(number => number * multiplier);
}
console.log(multiply(2, 1, 2, 3)); // Output: [2, 4, 6]
        
    

This example demonstrates how to use the rest operator to collect multiple function arguments into an array.


Example: Destructuring Arrays
Try yourself
        
            const [first, ...rest] = [1, 2, 3, 4, 5];
console.log(first); // Output: 1
console.log(rest);  // Output: [2, 3, 4, 5]
        
    

This example shows how to use the rest operator to collect remaining elements into an array during destructuring.


Example: Destructuring Objects
Try yourself
        
            const { a, b, ...rest } = { a: 1, b: 2, c: 3, d: 4 };
console.log(a);    // Output: 1
console.log(b);    // Output: 2
console.log(JSON.stringify(rest)); // Output: { c: 3, d: 4 }
        
    

This example demonstrates how to use the rest operator to collect remaining properties into an object during destructuring.


Important Notes

Here are some important notes and best practices when using the spread and rest operators in JavaScript:


Summary

The spread and rest operators, introduced in ES6, provide powerful and concise ways to work with arrays, objects, and function parameters. By understanding and using these operators, you can write cleaner and more maintainable JavaScript code.