<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/the-dark-side-of-foreach-why-you-should-think-twice-before-using-it-slhsbmoyn" -->

---
title: The Dark Side of 'Foreach()' : Why You Should Think...
description: Learn about the limitations of forEach() function in JavaScript, including performance issues, inability to escape the loop, and incompatibility with async...
canonical: https://daily.dev/posts/the-dark-side-of-foreach-why-you-should-think-twice-before-using-it-slhsbmoyn
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: The Dark Side of 'Foreach()' : Why You Should Think Twice Before Using It | daily.dev
og:description: Learn about the limitations of forEach() function in JavaScript, including performance issues, inability to escape the loop, and incompatibility with async...
og:url: https://daily.dev/posts/the-dark-side-of-foreach-why-you-should-think-twice-before-using-it-slhsbmoyn
og:image: https://api.daily.dev/og/posts/slhSbMOYn.png
og:image:alt: The Dark Side of 'Foreach()' : Why You Should Think Twice Before Using It
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# The Dark Side of 'Foreach()' : Why You Should Think Twice Before Using It

**[Community Picks](https://daily.dev/sources/community)** · 16 min read · 453 upvotes · 44 comments

## Summary

Learn about the limitations of forEach() function in JavaScript, including performance issues, inability to escape the loop, and incompatibility with async functions. Discover better options for working with arrays, such as map(), reduce(), and filter().

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://community.appsmith.com/content/blog/dark-side-foreach-why-you-should-think-twice-using-it>

## Community discussion

Top comments from developers on daily.dev.

**@husseincode** · 8 upvotes

> You can use break and countinue statement in forEach method
>
> To use continue statement in a forEach method :
> You can use return statement to skip the current iteration.
>
> Exaple:-
>
> const nums = [1, 2, 3, 4, 5];
>
> nums.forEach( num =>  {
>
>     if (num === 3) {
>      return;
> }
>
> })
>
> Output is [1, 2, 4, 5];
>
>
> If you want to use break statement in a forEach method .
> simply throw an error to break the loop
>
> try {
>
> const nums = [1, 2, 3, 4, 5];
>
> nums.forEach( num =>  {
>
>     if (num === 3) {
>      throw new Error();
> }
>
> })
>
> } catch(err) {
>     console.log(err);
> }
>
> Output is [1, 2];

**@arthurdurand** · 3 upvotes

> note: js performance optimizations like this rarely matter in frontend code, because you will rarely have to do something computationally heavy.
>
> never ignore readability of code when considering performance

**@davidhida** · 2 upvotes

> As beginner we don't think about using JS array native function and all the time we go with forEach loop method because it very handy and simple to use. But this article makes me think that I was right to use native array function except use forEach loop for very small set of data. I've loved this post. thanks for sharign, hope we'll get another helpful post like this one.

**@dilnawazkhan** · 1 upvotes

> Very well explained 🫶

**@leledallas** · 1 upvotes

> Nice post!
> I'll try to update my application with those advices, thank you!

---

Tags: [#javascript](https://daily.dev/tags/javascript), [#performance](https://daily.dev/tags/performance)

[View this post on daily.dev](https://daily.dev/posts/the-dark-side-of-foreach-why-you-should-think-twice-before-using-it-slhsbmoyn)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"The Dark Side of 'Foreach()' : Why You Should Think Twice Before Using It","url":"https://daily.dev/posts/the-dark-side-of-foreach-why-you-should-think-twice-before-using-it-slhsbmoyn","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/the-dark-side-of-foreach-why-you-should-think-twice-before-using-it-slhsbmoyn"},"datePublished":"2024-06-13T21:14:18.781Z","dateModified":"2024-08-23T15:26:10.144Z","description":"Learn about the limitations of forEach() function in JavaScript, including performance issues, inability to escape the loop, and incompatibility with async...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/7eaf76c959a2df91f938c2167bc4870e?_a=AQAEuiZ","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/7eaf76c959a2df91f938c2167bc4870e?_a=AQAEuiZ","isAccessibleForFree":true,"articleSection":"Community Picks","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Community Picks","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1655817725/logos/community","url":"https://daily.dev/sources/community"},"commentCount":44,"discussionUrl":"https://daily.dev/posts/the-dark-side-of-foreach-why-you-should-think-twice-before-using-it-slhsbmoyn","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":453},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":44}],"keywords":"javascript,performance","timeRequired":"PT16M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Community Picks","item":"https://daily.dev/sources/community"},{"@type":"ListItem","position":3,"name":"The Dark Side of 'Foreach()' : Why You Should Think Twice Before Using It"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/the-dark-side-of-foreach-why-you-should-think-twice-before-using-it-slhsbmoyn","comment":[{"@type":"Comment","text":"You can use break and countinue statement in forEach method\nTo use continue statement in a forEach method :\nYou can use return statement to skip the current iteration.\nExaple:-\nconst nums = [1, 2, 3, 4, 5];\nnums.forEach( num =\u0026gt;  {\nif (num === 3) {\n return;\n\n}\n})\nOutput is [1, 2, 4, 5];\nIf you want to use break statement in a forEach method .\nsimply throw an error to break the loop\ntry {\nconst nums = [1, 2, 3, 4, 5];\nnums.forEach( num =\u0026gt;  {\nif (num === 3) {\n throw new Error();\n\n}\n})\n} catch(err) {\nconsole.log(err);\n}\nOutput is [1, 2];","datePublished":"2024-06-14T12:43:53.980Z","url":"https://daily.dev/posts/slhSbMOYn#c-axZywcTdn","author":{"@type":"Person","name":"Hussein Sultan","url":"https://daily.dev/husseincode","image":"https://lh3.googleusercontent.com/a/ACg8ocLWM1nZNs9HcQsJDVfNFmka66lcGZRoi26G-NXKKrs5fiIt0r4=s96-c"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":8}},{"@type":"Comment","text":"note: js performance optimizations like this rarely matter in frontend code, because you will rarely have to do something computationally heavy.\nnever ignore readability of code when considering performance","datePublished":"2024-06-19T20:36:52.615Z","url":"https://daily.dev/posts/slhSbMOYn#c-Br6cVTMfo","author":{"@type":"Person","name":"Arthur DURAND","url":"https://daily.dev/arthurdurand","image":"https://avatars.githubusercontent.com/u/73845816?v=4"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":3}},{"@type":"Comment","text":"As beginner we don’t think about using JS array native function and all the time we go with forEach loop method because it very handy and simple to use. But this article makes me think that I was right to use native array function except use forEach loop for very small set of data. I’ve loved this post. thanks for sharign, hope we’ll get another helpful post like this one.","datePublished":"2024-08-10T11:40:42.344Z","url":"https://daily.dev/posts/slhSbMOYn#c-ZauuTsbqB","author":{"@type":"Person","name":"David Hida","url":"https://daily.dev/davidhida","image":"https://lh3.googleusercontent.com/a/AAcHTtetyGpfVgsXokrbbJqjSb1-QpbbHHl6r5IoM1Zn_j1HoWU=s96-c"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2}},{"@type":"Comment","text":"Very well explained 🫶","datePublished":"2024-08-19T04:40:03.956Z","url":"https://daily.dev/posts/slhSbMOYn#c-Ba6jFFg4i","author":{"@type":"Person","name":"Dilnawaz Khan","url":"https://daily.dev/dilnawazkhan","image":"https://media.daily.dev/image/upload/s---d7wfhSc--/f_auto/v1722436943/avatars/avatar_S2FAVRV8UEkR6OlBc1O3S"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1}},{"@type":"Comment","text":"Nice post!\nI’ll try to update my application with those advices, thank you!","datePublished":"2024-06-14T15:12:07.109Z","url":"https://daily.dev/posts/slhSbMOYn#c-vErZc1CdH","author":{"@type":"Person","name":"Emanuele Dall'Ara","url":"https://daily.dev/leledallas","image":"https://avatars.githubusercontent.com/u/71103219?v=4"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1}}]}
```

