Array sum

OSCC · JavaScript, Math, Array · Oct 22, 2020

Calculates the sum of two or more numbers/arrays.

const sum = (...arr) => [...arr].reduce((acc, val) => acc + val, 0);
sum(1, 2, 3, 4); // 10
sum(...[1, 2, 3, 4]); // 10

Recommended snippets