Use the Rest Operator with Function Parameters diff betwen reduce with 2 and 3 par

Why am I getting an error when using .reduce like args.reduce((total,amount)=>total+amount); but not when using args.reduce((total,amount)=>total+amount,0); can somebody explaing what that 0 is doing there in the end I looked everywhere but no success sry for bad engrish :slight_smile:

Your code so far


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

const sum = (function() {
“use strict”;
return function sum(…args) {
return args.reduce((total, amount) => total + amount);

};
})();
console.log(sum(1, 2, 3)); // 6

diffrence between that 2 codes

The second argument (the first argument is the function itself, so you are asking about the difference between 1 and 2 parameters, not 2 and 3) is the value you want to start with. So if it is set as 0, then the reduction for sum(1,2,3) goes 0 + 1, then 1 + 2, then 3 + 3. Without it it goes 1 + 2, 3 + 3. This doesn’t make any difference in this specific case (sum(1,2,3)), but it can, and the test are checking you have included it.