Use the Rest Operator with Function Parameters - A zero required?

Hi:

Was wondering if someone could explain why a comma followed by a zero is required in the ‘reduce’ function ((((( return args.reduce((a, b) => a + b, 0); )))))?. Thanks.

Your code so far


const sum = (function() {
  "use strict";
  return function sum(...args) {
    
    return args.reduce((a, b) => a + b, 0);
  };
})();
console.log(sum(1, 2, 3)); // 6

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-the-rest-operator-with-function-parameters

It is the initial value supplied to the reduce, it’s not required. When you call reduce without that initial value, in the first reduce iteration, your a will be set to the first element of the array. And if you supply it, your a will be the initial value (0 in this case).

In your case for example, you don’t need to supply that argument. I actually helped someone not long ago to solve a bug where he didn’t supplied the initial value in a case that was necessary. If you want to take a look, it may help you to understand it: Sum All Odd Fibonacci Numbers solution not passing all tests