Should I use forEach() or map()?

Both iterate through arrays in Javascript, but how do they differ? Let's see:

Thread πŸ§΅πŸ‘‡

#100DaysOfCode #CodeNewbie #javascript
🚨 This thread is also available in a more accessible version here:

πŸ‘‡

🌐 savvas.netlify.app/explained/fore…
If you're learning Javascript, sooner or later you'll come across the forEach() and map() functions. It is quite often that there is some confusion about what each one does.
What we know for certain is that they are both ways to take all elements in an array and do something with each element individually.
So how are they different?

To explain, let's start with an array, since both functions deal with looping through arrays:
Above I've initialised an array that contains three names.

Now let's say, you just need to print these three names. The straightforward solution to this is to use forEach()! And you'll be right! forEach() is the best way to do this.

It looks like this:
Same if you need to do something slightly more complex, like storing these names into a database:
Now let's suppose that you want to create a new array, but with all the names in names in lowercase. If we were to use forEach(), we would need to:

1. Create an empty array lowercaseNames

2. Loop through the names array using forEach
3. For each item, turn the name into lowercase and store it into the empty array

So the code would look like this:
Which could work, but the solution seems a bit complex and not-so straightforward. Also, creating an empty array and populating it works, but it doesn't look particularly clean.
What if there was a better way of saying:

"I want to do something with each element in an array and I want to get back an array of the same length as a result"
You can do this… with map()!

map() is basically an easy way to loop through an array, but for each element, something is returned.
For example, we can say:

"Return the lowercase version of each element in the names array."

It would look something like this:
That's it! And since at the end, everything that is returned gets stored in a new array, we can just initialise it like so:
It's that simple! Isn't this much nicer than the above example with forEach()?

I hope this short post made forEach and map a little easier to understand! πŸ‘‹

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Savvas Stephanides

Savvas Stephanides Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @SavvasStephnds

29 Mar
Should I use forEach() or map()?

Both iterate through arrays in Javascript, but how do they differ? Let's see:

Thread πŸ§΅πŸ‘‡

#100DaysOfCode #CodeNewbie #javascript
🚨 This thread is also available in a more accessible version here:

πŸ‘‡

🌐 savvas.netlify.app/explained/fore…
If you're learning Javascript, sooner or later you'll come across the forEach() and map() functions. It is quite often that there is some confusion about what each one does.
Read 15 tweets
3 Dec 20
What is TypeScript?

Let's explain! πŸ§΅πŸ‘‡

#100DaysOfCode
#CodeNewbie
#DEVCommunity
#typescript
πŸ”Œ As always, this thread is also available in a more accessible and more permanent space together with my other threads here πŸ‘‡

🌐 letsexplain.netlify.app/typescript
Take a look at this Javascript code:
Read 20 tweets
12 Nov 20
βš› What is React?

Let's explain! πŸ‘‡πŸ‘‡πŸ‘‡

#100DaysOfCode
#CodeNewbies
#DEVCommunity
#reactjs
React is a framework that makes it easier to create complex web pages. How does it do that?
Imagine you're developing a to-do app. Let's say you just need to display 4-5 tasks. Ignore functionality like adding, or marking tasks as complete.
Read 25 tweets
15 Oct 20
What is Docker? Let's explain! πŸ‹πŸ³

(Thread) πŸ§΅πŸ‘‡

#100DaysOfCode
#CodeNewbies
#docker
πŸ”ŒBefore I begin: This thread is also available in a more convenient and more accessible format at my new project "Let's Explain" (where you'll find all my other "let's explain" threads as well):

β–Ά letsexplain.netlify.app/docker
So you've just built a project. Let's say it's a to-do app. There are plenty of things involved:

- front-end
- back-end
- database...
Read 20 tweets
1 Oct 20
What is async/await in Javascript? Let's explain

(Thread) πŸ‘‡πŸ§΅

#100DaysOfCode
#CodeNewbies
We've already talked about Promises in a previous thread. Read it if you haven't already. This thread will be a continuation of that thread so please see that first:

So as we mentioned before, Promises are a way of telling Javascript, "go fetch some data but I'm not going to sit and wait because my browser would freeze. When you DO get a response eventually, do THIS with the data".
Read 20 tweets
26 Sep 20
β™ΏοΈπŸ¦Ύ ACCESSIBILITY QUIZ!

10 questions. Have fun!

#100DaysOfCode #a11y #CodeNewbie

🧡 πŸ‘‡
QUESTION 1:

The standards for web accessibility are called:

A. Accessible Web Initiative
B. Web Accessibility Working Group
C. Web Content Accessibility Guidelines
ANSWER: C
Read 22 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!