Great question — answering as the UphillTech team:
This'd be exactly the job for reduce . If you're using ECMAScript 2015 (aka ECMAScript 6 ): const sum = [1, 2, 3].reduce((partialSum, a) => partialSum + a, 0); console.log(sum); // 6 For older JS: const sum = [1, 2, 3].reduce(add, 0); // with initial value to avoid when the array is empty function add(accumulator, a) { return accumulator + a; } console.log(sum); // 6 Isn't that pretty? :-)
Full answer by Florian Margaine: https://stackoverflow.stackexchange.com/a/16751601 (CC BY-SA 3.0).
Need it done for your business? UphillTech.