DEV Community

Discussion on: Why is using javascript “for loop” for array iteration a bad idea?

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
_prosen profile image
Prosen Ghosh

I use loop or array methods depending on what/which purpose i'm using it for.

In the below code example for loop or forEach() will not going to serve my purpose in a easy to readable/understandable way.

function ListItems({ items }) {
  const listItems = items.map((item) =>
    <li>{item}</li>
  );
  return (
    <ul>{listItems}</ul>
  );
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
hasnaindev profile image
Muhammad Hasnain

Higher order functions makes code cleaner and easy to understand. forEach may be simple but filter, map or reduce makes tedious operations very easy to implement.

Collapse
 
_prosen profile image
Prosen Ghosh

Yes, forEach() method also a higher order function.

But as i mention before that, i use those methods depending on the context where it fits the most.

Collapse
 
lepinekong profile image
lepinekong

for of works with async not forEach