#100DaysOfCode Day 8: forEach & Map

Shawn McMahon
2 min readJul 19, 2021

Day 8: forEach and Map

Yesterday I wrote about for loops to give us a background into the usefulness of array iterator methods. Today I will explore 2 array iterator methods to understand what we learned and why array iterator methods make my job as a developer a lot easier.

6 bananas in a row melting into the ground
Source: https://www.pexels.com/photo/banana-fruits-2964916/

forEach

forEach is like the parent of all array iterator methods. If you understand what forEach does, you will have a basic understanding of what all array iterator methods do under the hood.

For each will take in an array as an input and perform the logic given within the callback function to every index in the array.

Consider the code snippet below. On line 1278, we have an array of someNumbers. On line 1287, we see that for each is taking in the input of the someNumbers array. Inside its parameter, it takes in a callback function where we define what logic needs to happen to each index. In this example we want to print a string ‘I have x hats’, with x corresponding to the current value in the index. And presto — we created 5 separate strings with one line of code without having to hard code that string for each index of the array.

forEach is pretty simple. We give the callback function some logic and that logic is performed for every index in the array

Example A

Map

Map is pretty similar to forEach but it has one key difference — it returns a new array.

Consider line 1280 from above. The syntax of map requires us to declare a new variable at the beginning of the line. Since map returns an array it needs to be returned or stored somewhere, which is why we make this variable declaration. On line 1281, we declare that we want to multiply each index of the array by 2. On line 1284, a new array is returned with exactly what we are expecting. Notice we had to console the variable name of the new array since map does not manipulate data from the original array.

I kept these examples simple to get these concepts across. It may not look like much right now but these methods make my job so much easier! Think about what the code would look like if I have to type out each statement individually? That would be rough!

Array iterator methods provide a sweet, simple way to manipulate data dynamically on a large scale very quickly!

Tomorrow we will discover two more iterator methods: filter and find.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Shawn McMahon
Shawn McMahon

Written by Shawn McMahon

Software Engineer, Snowboarder, Music Enthusiast

No responses yet

Write a response