What are the benefits of using spread syntax in JavaScript and how is it different from rest syntax?
TL;DR
Spread syntax (...
) allows an iterable (like an array or string) to be expanded into individual elements. This is often used as a convenient and modern way to create new arrays or objects by combining existing ones.
Operation | Traditional | Spread |
---|---|---|
Array cloning | arr.slice() | [...arr] |
Array merging | arr1.concat(arr2) | [...arr1, ...arr2] |
Object cloning | Object.assign({}, obj) | { ...obj } |
Object merging | Object.assign({}, obj1, obj2) | { ...obj1, ...obj2 } |
Rest syntax is the opposite of what spread syntax does. It collects a variable number of arguments into an array. This is often used in function parameters to handle a dynamic number of arguments.
Spread syntax
ES2015's spread syntax is very useful when coding in a functional paradigm as we can easily create copies of / merge arrays or objects without resorting to Object.create
, Object.assign
, Array.prototype.slice
, or a library function. This language feature is used often in Redux and RxJS projects.
Copying arrays/objects
The spread syntax provides a concise way to create copies of arrays or objects without modifying the originals. This is useful for creating immutable data structures. However do note that arrays copied via the spread operator are shallowly-copied.
Merging arrays/objects
The spread syntax allows you to merge arrays or objects by spreading their elements/properties into a new array or object.
Passing arguments to functions
Use the spread syntax to pass an array of values as individual arguments to a function, avoiding the need for apply()
.
Array vs object spreads
Only iterable values like Array
s and String
s can be spread in an array. Trying to spread non-iterables will result in a TypeError
.
Spreading object into array:
On the other hand, arrays can be spread into objects.
Rest syntax
The rest syntax (...
) in JavaScript allows you to represent an indefinite number of elements as an array or object. It is like an inverse of the spread syntax, taking data and stuffing it into an array rather than unpacking an array of data, and it works in function arguments, as well as in array and object destructuring assignments.
Rest parameters in functions
The rest syntax can be used in function parameters to collect all remaining arguments into an array. This is particularly useful when you don't know how many arguments will be passed to the function.
Provides a cleaner syntax than using the arguments
object, which is unsupported for arrow functions and represents all arguments whereas the usage of the rest syntax below allows remaining
to represent the 3rd argument and beyond.
Note that the rest parameters must be at the end. The rest parameters gather all remaining arguments, so the following does not make sense and causes an error:
Array destructuring
The rest syntax can be used in array destructuring to collect the remaining elements into a new array.
Object destructuring
The rest syntax can be used in object destructuring to collect the remaining properties into a new object.