DEV Community

Discussion on: Understanding Reduce in JavaScript

Collapse
 
macarie profile image
Raul • Edited

This is a very useful article, well done! I think I'll use it as the default goto when my friends will ask about it 👀
I like that you showed how map and filter can be used instead of reduce, and then made more of a real-world example 😃

Two things are not quite right tho...
One is that in the header image, and the other image, on the first line there's "using map", but it should be "a filter" I guess.
The other thing is the following comment: // Note: the initialAccValue is optional and defaults to 0 when omitted.
Yes, initialAccValue is optional, but its default is not 0, it's the first element of the array, and the reducer won't even be called with it, so it's risky, even riskier if the array is empty as it will throw a TypeError.

Collapse
 
m0nica profile image
Monica Powell

Hi Raul, Thank you for the feedback. I've updated the comment to correct the mistake re: the default value for initialAccValue and may add more information later re: how it behaves when the initialAccValue is omitted.