The Spread Operator

The spread operator looks the same as the symbol (...) used for the rest parameter, but it appears on the calling side of functions instead of on the parameter or receiving side. The intention of the spread operator is the opposite of that of the rest parameter—spread breaks a collection into discrete values whereas rest gathers discrete values into an array. Since they are used in two different contexts, there should be no confusion.

Suppose we have a greet() function that takes a rest parameter, like so:

 const​ greet = ​function​(...names) {
  console.log(​'hello '​ + names.join(​', '​));
 };

If we have discrete variables, we can readily send them to the greet() function:

 const ...

Get Rediscovering JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.