How to Use the Spread Operator (…) in JavaScript
The spread operator is a useful and quick syntax for adding items to arrays, combining arrays or objects, and spreading an array out into a function’s arguments.
What is the spread operator?
In JavaScript, spread syntax refers to the use of an ellipsis of three dots (…
) to expand an iterable object into the list of arguments.
“When
...arr
is used in the function call, it ‘expands’ an iterable objectarr
into the list of arguments.” — JavaScript.info
The spread operator was added to JavaScript in ES6 (ES2015), just like the rest parameters, which have the same syntax: three magic dots …
.
What is ...
used for?
“Spread operator to the rescue! It looks similar to rest parameters, also using
...
, but does quite the opposite.” — JavaScript.info