JavaScript has seen massive improvements to its syntax and functionality over the last few years with the additional of a bunch new features. One of the most useful of those new features is the Spread operator
( aka the ... operator)
Spread is super useful, but its syntax is not particularly meaningful upon first glance. It doesn't really tell you much as to what it does. Fortunately, once you learn the 3 main uses for Spread, it will become on of your favorite tools in the JS toolbox!
According to MDN:
Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
Basically, there are three places you can use spread:
When passing arguments to a function, we can use spread to expand an itererable element. Rather than having to pass each argument individually, we can "spread" out each item as its own argument.
Math.min is a function which accepts any number of arguments and returns the minimum value. If we wanted to find the minimum value of an array, it doesn't work to pass the entire array in:
const temperatures = [76,72,68,79,54,65];
Math.min(temperatures)
▹ NaN
Instead, we can use spread to expand our array, passing in each element as an argument:
const temperatures = [76,72,68,79,54,65];
Math.min(...temperatures)
//the same as Math.min(76,72,68,79,54,65)
▹ 54
Here's one more example using console.log. If we pass an entire array to console.log
const tvShows = ["Six Feet Under", "Chernobyl", "Black Mirror", "Fleabag" ]
console.log(tvShows);
This is what we see: