https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-the-rest-parameter-with-function-parameters

const sum = (…args) => {

return args.reduce((a, b) => a + b, 0);

}

*My only doubt is why do we add the ‘0’ at the end*

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-the-rest-parameter-with-function-parameters

const sum = (…args) => {

return args.reduce((a, b) => a + b, 0);

}

*My only doubt is why do we add the ‘0’ at the end*

It’s initial value, think about the below. Go to the MDN maybe, there are bunch of examples.

```
const sum = (...args) => {
return args.reduce((a, b) => a + b, 0);
}
console.log(sum(2,3));//5
const sum1 = (...args) => {
return args.reduce((a, b) => a + b, 1);
}
console.log(sum1(2,3));//6
```

Ok thanks

Got it!

But this task requires 0 to solve it.

?

Yup, I believe that’s because of the testcase: sum()

```
const sum = (...args) => {
return args.reduce((a, b) => a + b);
}
console.log(sum());
/*
TypeError: Reduce of empty array with no initial value
at Array.reduce (<anonymous>)
.......
*/
```

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.