With only two parameters passed to reduce , it will use the first and second elements of the array as arguments for the first function call.
function addDouble(memo, num) {return (num * 2) +memo } [1,2,3].reduce(addDouble, 0)
Usually you pass in the initial value, but many operations have the same result when you start without an identity element . For example:
function add(a, b) { return a+b; } function double(a) { return 2*a; } [1,2,3].map(double).reduce(add) == [1,2,3].map(double).reduce(add, 0)
See also docs for native reduce() .
Bergi
source share