DEV Community

Shariful Ehasan
Shariful Ehasan

Posted on

4 2 1 1

Which JavaScript Loop Do You Use the Most and Why?

JavaScript gives us multiple ways to loop through data and execute repetitive tasks, each with its own strengths and ideal use cases:

for, while, do...while
for...in, for...of
.forEach(), .map(), .filter(), .reduce()

👉 Which one do you reach for the most in your projects?

Top comments (17)

Collapse
 
brense profile image
Rense Bakker

Tbh I barely use just loops anymore. It's almost always .map in combination with filter or reduce if needed. If mapping is not required I use forEach or for of depending on where the data comes from. If the data comes from a source outside of the source code, I always use for of for performance reasons. Oh and sometimes while loops when doing recursive stuff, like with generator functions.

Collapse
 
thecodeliner profile image
Shariful Ehasan

Using .map, .filter, and .reduce definitely leads to more declarative and readable code in most cases.

Collapse
 
basselalsayed profile image
Bassel Al-Sayed

I don't use reduce anymore, instead I've adopted the habit of using Object.fromEntries:

const customEventNames = ['click', 'pageload', 'unmount'];

// before
const customEvents = customEventNames.reduce((acc, name) => {
  acc[name] = new CustomEvent(name);
  return acc;
}, {});

// after
const custmomEvents = Object.fromEntries(
  customEventNames.map((name) => [name, new CustomEvent(name)])
);
Enter fullscreen mode Exit fullscreen mode

I used to think using reduce was very fancy, imo now this is so much cleaner

Collapse
 
thecodeliner profile image
Shariful Ehasan

reduce can be powerful but often feels a bit cryptic for building objects. Object.fromEntries + map is way more readable, especially for teammates going through your code later. Nice switch! 👌

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

for (const item of collection) {} is the way to go :)

Collapse
 
miketalbot profile image
Mike Talbot ⭐

for or for...of if I'm looking for speed. map, then filter, then reduce by the look of things for data manipulation.

Collapse
 
nevodavid profile image
Nevo David

nice, i always default to forEach out of habit but maybe i should branch out more - you ever feel like certain habits just stick even when you know there’s other options around?

Collapse
 
temz profile image
TemoSulava

ForEAch has specific usecase, you never modify arrays?

Collapse
 
pengeszikra profile image
Peter Vivo • Edited

.filter()
.map()
await for

Collapse
 
lovit_js profile image
Lovit

for...of
Because of eslint warning 😄

Collapse
 
karo_yousefi profile image
Karo Yousefi

.map() since I'm mostly writing React code.

Collapse
 
nathan_tarbert profile image
Nathan Tarbert

Honestly I hit forEach way too much by default, kinda stuck in my habits but it gets the job done every time.

Collapse
 
thecodeliner profile image
Shariful Ehasan

forEach is super intuitive and readable, so it's easy to fall back on it.

Collapse
 
himeshdua profile image
Himesh Dua

I have never used while loop. In my code its mostly .map because I mostly write Next code

Postmark Image

"Please fix this..."

Focus on creating stellar experiences without email headaches. Postmark's reliable API and detailed analytics make your transactional emails as polished as your product.

Start free

Join the Runner H "AI Agent Prompting" Challenge: $10,000 in Prizes for 20 Winners!

Runner H is the AI agent you can delegate all your boring and repetitive tasks to - an autonomous agent that can use any tools you give it and complete full tasks from a single prompt.

Check out the challenge

DEV is bringing live events to the community. Dismiss if you're not interested. ❤️