The spread operator allows you to quite literally “spread” out an array. This can be used to transform an array into a list of arguments or even combine two arrays together. You could also use it to form a list of arguments to a function too. Check it out:
let data = [1,2,3,4,5];
console.log(...data);
--> 1 2 3 4 5
let data2 = [6,7,8,9,10];
let combined = [...data, ...data2];
console.log(...combined);
--> 1 2 3 4 5 6 7 8 9 10
console.log(Math.max(...combined));
--> 10
In the first example we show how the spread operator works on an array and turns each item into an individual element. The second example combines the contents of two arrays together by creating a new temporary array containing both contents. The last example illustrates how the spread operator can turn an array into a list of arguments to a function. The Math.max
returns the highest number in a list of arguments passed to it. One of those arguments was 10
which is the highest.
Source
https://levelup.gitconnected.com/6-javascript-code-snippets-for-solving-common-problems-33deb6cacef3