JavaScript > ES6 and Beyond > New Syntax and Features > Spread and rest operators
Understanding Spread and Rest Operators in JavaScript
This code snippet illustrates the use of spread and rest operators in JavaScript (ES6+). Spread allows you to expand elements of an iterable (like an array) into places where multiple elements are expected. Rest allows you to collect multiple elements into a single array.
Spread Operator Basics
The spread operator (...) expands an iterable (array, string, or object) into individual elements. In the first example, it's used to combine two arrays into a single array. In the second example, it merges two objects into one. This creates a new object, without modifying the originals.
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const combinedArray = [...arr1, ...arr2];
console.log(combinedArray); // Output: [1, 2, 3, 4, 5, 6]
const obj1 = { a: 1, b: 2 };
const obj2 = { c: 3, d: 4 };
const combinedObject = {...obj1, ...obj2};
console.log(combinedObject); // Output: { a: 1, b: 2, c: 3, d: 4 }
Rest Operator Basics
The rest operator (...) collects multiple arguments into a single array. In this example, the `sum` function can accept any number of arguments, which are then collected into the `numbers` array. Inside the function, we can iterate over this array to calculate the sum.
function sum(...numbers) {
let total = 0;
for (const number of numbers) {
total += number;
}
return total;
}
console.log(sum(1, 2, 3, 4, 5)); // Output: 15
Concepts Behind the Snippet
The key concept behind these operators is to simplify array and object manipulation, along with making function arguments more flexible. Before ES6, combining arrays or passing variable arguments required more verbose methods. Spread and rest provide a more concise and readable syntax.
Real-Life Use Case: Cloning Arrays and Objects
Spread can be used to create shallow copies of arrays and objects. A shallow copy means that the new array/object contains copies of the original's elements/properties. If the original contains nested objects or arrays, those nested structures are still referenced by both the original and the copy. Modifying a primitive property in the clone doesn't affect the original, but modifying a nested object/array will affect both. For deep cloning, you may need to use techniques like `JSON.parse(JSON.stringify(object))` (with limitations) or a dedicated deep clone library.
const originalArray = [1, 2, 3];
const clonedArray = [...originalArray]; // Creates a shallow copy
clonedArray[0] = 10;
console.log(originalArray); // Output: [1, 2, 3] (original is unchanged)
console.log(clonedArray); // Output: [10, 2, 3] (clone is modified)
const originalObject = { a: 1, b: 2 };
const clonedObject = {...originalObject};
clonedObject.a = 10;
console.log(originalObject); // Output: { a: 1, b: 2 }
console.log(clonedObject); // Output: { a: 10, b: 2 }
Real-Life Use Case: Passing arguments to functions
The spread operator allows you to pass the elements of an array as individual arguments to a function. This is useful when the function expects individual arguments rather than an array.
function myFunction(a, b, c) {
console.log(a, b, c);
}
const myArray = [1, 2, 3];
myFunction(...myArray); // Output: 1 2 3
Best Practices
Interview Tip
Be prepared to explain the difference between spread and rest operators. Know their use cases and how they can simplify code. Understand the concept of shallow copying and its implications. Be aware of the limitations of `JSON.parse(JSON.stringify(object))` for deep cloning (e.g., it won't work with functions or circular references).
When to use them
Use the spread operator when you need to expand an iterable into individual elements, like combining arrays, cloning arrays/objects, or passing arguments to functions. Use the rest operator when you need to collect multiple arguments into a single array, typically in function definitions.
Memory footprint
The spread operator creates new arrays and objects. This means it allocates new memory. While generally efficient, creating many large copies can impact performance and memory usage. Consider alternatives like modifying arrays in place (if appropriate) if memory efficiency is critical. The rest operator generally has minimal impact on memory footprint as it primarily collects existing arguments into an array.
Alternatives
Spread and rest offer cleaner and more versatile alternatives.
Pros
Cons
FAQ
-
What's the difference between spread and rest?
Spread expands an iterable into individual elements, while rest collects multiple arguments into a single array. -
Does spread create a deep copy?
No, spread creates a shallow copy. Changes to nested objects/arrays within the copy will affect the original. -
Can I use spread with strings?
Yes, you can spread a string into an array of characters, e.g., `[...'hello']` results in `['h', 'e', 'l', 'l', 'o']`. -
Are there performance considerations when using spread?
Yes, creating many large copies can impact performance and memory usage. Be mindful of this, especially in performance-critical sections of your code.