Files
30-seconds-of-code/snippets/sum.md
2018-12-05 17:53:52 +08:00

15 lines
300 B
Markdown

### sum
Returns the sum of two or more numbers/arrays.
Use `Array.prototype.reduce()` to add each value to an accumulator, initialized with a value of `0`.
```js
const sum = (...arr) => [...arr].reduce((acc, val) => acc + val, 0);
```
```js
sum(1, 2, 3, 4); // 10
sum(...[1, 2, 3, 4]); // 10
```