DEV Community

Discussion on: 🔥 What's Hot in Web Development? — Weekly Picks #112

Collapse
lukaszahradnik profile image
Lukáš Zahradník • Edited on

Stop using for loop, here are other cool options

I would argue that the linked post is absolutely misleading.
It promotes map, filter and reduce as futuristic functions even though they exist for few years (since ES6) and are supported even by IE9 (such a futuristic browser indeed).

It says that they have these advantages over for loops and forEach:

  • It’s easy to write and others can interpret it easily
  • It’s easy to maintain, extend, and test
  • You can write pure functions without any side effects
  • Helps you thinking in terms of functional programming
  • If you ever plan to use libraries like RxJS, it will surely help

I'm not sure about the last point, but first three are wrong.

Also author decided to write the most verbose version of the for loop, instead of using for of version. Why he is not using more verbose function syntax instead of arrow functions?

Also he provides the most simplistic example of the usage, which is more readable with map, filter and reduce, but it doesn't reflect the real world usage.

None of these functions is equivalent to for loops (for example how would you break out of map?).

So, you shouldn't really stop using for loops. Learn how and where to use loops, map, filter and reduce and use them where are they appropriate (You will find, that there aren't really many use cases for reduce).