0

Can anyone tell me what means the (...numbers) when we create funtion parasiteSum and give the parameters to the function. If anyone can explain me for what we use this syntax and why we use, thank you soo much.

function sumAllNumbers(array) {
  let reducer = (currentValue, totalValue) => currentValue + totalValue;
  return array.reduce(reducer);
}

function parasiteSum(...numbers) {
  return sumAllNumbers(numbers);
}

parasiteSum(1, 2, 3); 
Lacko35
  • 3
  • 2
  • It collects the arguments given into a single Array. It's called "rest" syntax, as in the collection will hold the rest of the arguments. It can only be the last parameter defined. –  Sep 03 '20 at 16:36
  • Notes : be careful, Rest syntax looks exactly like spread syntax. In a way, rest syntax is the opposite of spread syntax. Spread syntax "expands" an array into its elements, while rest syntax collects multiple elements and "condenses" them into a single element. – Rio A.P Sep 03 '20 at 16:38
  • Also, pass an initial value in `reduce` as the second argument: `array.reduce(reducer, 0)`. Otherwise, if it is called on empty array, it will throw an error – adiga Sep 03 '20 at 17:04

0 Answers0