Comparing JS iteration methods (map, filter, forEach, reduce + loops)
Tags: javascript, methods, iteration, loop
Published: 2020-09-05 20:25:38

---
title: Comparing JS iteration methods (map, filter, forEach, reduce + loops)
published: true
date: 2019-04-20 10:56:00 UTC
tags: javascript,methods,iteration,loop
canonical_url: 
---

_Part III of “_ _Weird questions from my first month of coding”_

There are several ways to iterate through arrays in JavaScript. They all have one thing in common: they execute a given function in _each item_ of your array; the difference is what you get in return.

Here I assume you know _how_ to use each iterator. It’s ok if you get confused by them or if you’re note sure of _when_ to use them, though. It took me a few weeks to understand the proper use of different iterators, and I hope this post will help you clear this out.

![](https://cdn-images-1.medium.com/max/527/1*AgYSfRbK6jtTqiZosUp19g.png)<figcaption>The table I wish I had when I learned JavaScript iteration methods.</figcaption>

### .map = transform

Use map when you want to **transform** an array without changing its original values.

![](https://cdn-images-1.medium.com/max/264/1*WOKdroo-6B0G6s0QJNssAQ.png)

**IRL** (In real life): You can use this method to iterate through API data to show each entry in your website — instead of hard coding them in each div, for example.

### .filter = select

Use filter when you want to **select** certain elements in your array. When an element meets the conditions given in your function (in other words, it’s true), it is included in the returned array:

![](https://cdn-images-1.medium.com/max/290/1*sn_XOVfg0kxyDH1xv3Gh4A.png)

**IRL:** If you have a database of clients and need to find only those over 18 yo, you can use filter to do it.

### .forEach = execute

forEach is useful when you simply need to **run a function** through each item in the array, with no need to get a return.

![](https://cdn-images-1.medium.com/max/366/1*r5ddWJiwKdxrHLWFbyQAxA.png)<figcaption>You can see that <em>oddsPlusOne</em> is processing the requested function but, in the end, it’s undefined.</figcaption>

**IRL:** You can use forEach to process and save data in your database.

### .reduce = reduce

If you need your **array to be reduced to a single value** , reduce is a very elegant solution:

![](https://cdn-images-1.medium.com/max/296/1*E4ztdJyxvRz9LYoe3ijCcg.png)<figcaption>In this example, the function is multiplying 1 x 3 x 5 x 7</figcaption>

**IRL:** The classic example is to get the sum of numbers in a certain array. For example, if you have a catalog of books in JSON format, you can check how many of those books were published when your grandma was your age.

![](https://cdn-images-1.medium.com/max/707/1*rSeebWb5AllYnqdVzy2KEQ.png)<figcaption>Would emojis explain it better?</figcaption>

### for loop = until condition is false

It’s useful to use this iteration when you know how many times you want to execute a function.

![](https://cdn-images-1.medium.com/max/189/1*Msaj45agPPDAh8nLxGQ1IQ.png)<figcaption>Here, we add 10 to numbers up to 9. When the condition becomes false (meaning, i=10), the function stops executing.</figcaption>

### while loop = while condition is true

If you don’t know in advance how many times to execute a function, a while loop might be a good idea. In real life, this iteration could be used to slideshow your phone pictures until you tap stop, for example. Or if your gaming character keeps running for as long as you press an arrow key. In both cases, you don’t know beforehand when the user will interrupt the loop. But be careful: if you set while condition to true, you may accidentally create an infinite loop Oo

![](https://cdn-images-1.medium.com/max/480/0*mvOVwzZ4zjH2HsWz.jpg)

### However…

Please note that there might not be _one_ right answer to your function. If you look closely, you’ll see that a for loop can well do the job of a map or reduce method, or that while loops work fine replacing for loops. But the more we code, the more we see that some options fit better than others. You may pick a certain method to make your code more readable, or you might decide for another one for increased performance. In any case, if you know your tools you have a larger set of options to make your code more efficient and friendly to the machines and to your fellow humans too :)

> _“Weird questions from my first month of coding” series_  
> Part I: [How array indexes & values work in for loops](https://medium.com/@manumagalhaes/how-array-indexes-values-work-in-for-loops-289163855f87?source=friends_link&sk=72f44ce790e8381c6ff36f69f444abda)  
> Part II: [How to write functions that go inside a filter method](https://medium.com/@manumagalhaes/how-to-write-functions-that-go-inside-a-filter-method-ffefe1b83e9c?source=friends_link&sk=1c91261f94c8aeb8a7dcbc137277047b)

* * *