Setting initialValue of Array.prototype.reduce with a default argument

Zacqary Adam Xeper zacqary.xeper at
Wed Feb 24 23:13:30 UTC 2016

Not sure if this has been proposed, discussed, and soundly rejected before,
but I wasn't able to find any mention of it in the archives.

Let's say I'm using Array.prototype.reduce with an initialValue to convert
an array into an object, like this:

[0,1,2,3,4].reduce((result, val) => {
    let key = val % 2 ? "even" : "odd";
    return {...result, [key]: val};
}, {})

The problem with that is it takes you all the way to the end of the
function to find out what the initial value of `result` is (in this case,
an object). This is bad for code readability.

Now that ES2015 has introduced default arguments, it would make a lot more
sense if I could say:

array.reduce( (result = {}, val) => {} )

I know this presents a backwards-compatibility issue. Is that surmountable?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list