I have a general question that isn't specific to React, so I hope it's okay to ask here. I always believed that the spread operator flattens an array. However, in the following sum function that sums the arguments, we can use .apply to pass in the values:
function sum() {
return arguments.reduce((total, number) => total + number, 0);
}
var values = [2, 4, 8, 12, 16];
console.log(sum.apply(null, values));
I initially thought that we could simply add the spread operator to the function and use it to flatten the array, enabling us to use call. (I understand that call wouldn't be necessary in this case, but I was surprised because I thought the spread operator flattened the array:
function sum() {
return [...arguments].reduce((total, number) => total + number, 0);
}
var values = [2, 4, 8, 12, 16];
console.log(sum.call(null, values));
However, this returns the string 02,4,8,12,16