0

I found this example in MDN about .reduce() method. what does currentValue,initialValue do/mean here?

const array1 = [1, 2, 3, 4];

// 0 + 1 + 2 + 3 + 4
const initialValue = 0;
const sumWithInitial = array1.reduce(
  (accumulator, currentValue) => accumulator + currentValue,
  initialValue /
```//what does currentValue,initialValue do . I would understand if it had accumulator+currentValue. But what does the comma ,initialValue do/ mean here? I've never seen a+b,c. I don't know what b,c would do in this case.
);

`console.log(sumWithInitial);
`// Expected output: 10

I try reading the MDN documentation to understand more things , but I couldn't understand that part. 
mikeli
  • 1
  • 2
  • It's shorthand for `array1.reduce((accumulator, currentValue) => { return accumulator + currentValue; }, initialValue);`. The symbol `=>` is a "fat arrow" function, where you don't need `return` nor wrapping `{` `}`. So `currentValue,initialValue` isn't one thing, `currentValue` is the end of the function definition, then `initialValue` is the second argument passed to `reduce()`. – Andy Ray Jul 18 '23 at 23:39
  • The `,` in `b,c` is a comma that separates the first argument of the `.reduce()` method, that being the arrow function `(accumulator, currentValue) => accumulator + currentValue` in your case, and the second argument, which is `initialValue` – Nick Parsons Jul 18 '23 at 23:39

0 Answers0