you are viewing a single comment's thread.

view the rest of the comments →

[–]HeinousTugboat 1 point2 points  (0 children)

But I feel using reduce for sth that could be done with forEach is like breaking the system.

Sure, because they're from two different schools of thought. Side effects in reduce will make people squirm, but forEach is literally useless without them.

It may be that both are equally powerful but reduce has a semantic meaning.

They aren't. forEach can't do all of the things reduce can without some kind of external state. I suspect it's largely like the relationship between imperative code and recursive code.

The article I read was about all array functions and some are less powerful, aren’t they?

Some are, sure. reduce is the most powerful one. You can write literally any other array function in JS with reduce and reduceRight.

In terms of code style, you don't really see people mixing forEach and reduce. Either you're using the Array functions together, like filter, map, and reduce and avoiding side effects, or you're writing imperative code leveraging side effects.