3 things you didn’t know about the forEach loop in JS

Tiberiu Oprea
Jun 26, 2018 · 3 min read
Image for post
Image for post

Do you think you know exactly how the forEach loop works in JS?

Well, these were my thoughts until recently: “just a regularfor loop where you can easily use break or return or continue“.

Today, I’ll show you 3 things which you might have not known about the forEach loop.

1. ‘return’ doesn’t stop looping

Do you think the code below would print 1 2 and then stop?

No, it won’t. If you come from a Java background, you would probably ask yourself how is that possible?

The reason is that we are passing a callback function in our forEach function, which behaves just like a normal function and is applied to each element no matter if we return from one i.e. when element is 2 in our case.

From the official MDN docs:

There is no way to stop or break a forEach() loop other than by throwing an exception. If you need such behavior, the forEach() method is the wrong tool.

Let’s rewrite the code from above:

The returnstatement won’t make any difference, as we apply the function to each element at each iteration, hence it doesn’t care if you exited once i.e. when element is 2.

2. You cannot ‘break’

Do you think aforEach loop would break in the example below?

No, it won’t even run because the break instruction is not technically in a loop.

Solution?

Just use a normal for loop. Nobody would laugh at you.

3. You cannot ‘continue’

Do you expect the code below to skip printing 2 to the console and only show 1 3 4 ?

No, it won’t even run because the continue instruction is not in a loop, similar to the break instruction.

Solution?

Just use a normal for loop again.

That was it! Hope you learned something new today.


If you are a business owner or a marketer who wants to improve 📈 his or her email deliverability, then you need to check out www.removebounce.com 👨‍💻.

Frontend Weekly

It's really hard to keep up with all the front-end…

Thanks to

Tiberiu Oprea

Written by

Helping marketers improve their email deliverability | www.removebounce.com

Frontend Weekly

It's really hard to keep up with all the front-end development news out there. Let us help you. We hand-pick interesting articles related to front-end development. You can also subscribe to our weekly newsletter at http://frontendweekly.co

Tiberiu Oprea

Written by

Helping marketers improve their email deliverability | www.removebounce.com

Frontend Weekly

It's really hard to keep up with all the front-end development news out there. Let us help you. We hand-pick interesting articles related to front-end development. You can also subscribe to our weekly newsletter at http://frontendweekly.co

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store