There's no built-in ability to break in forEach. To interrupt execution you would have to throw an exception of some sort. eg.

var BreakException = {};

try {
  [1, 2, 3].forEach(function(el) {
    console.log(el);
    if (el === 2) throw BreakException;
  });
} catch (e) {
  if (e !== BreakException) throw e;
}

JavaScript exceptions aren't terribly pretty. A traditional for loop might be more appropriate if you really need to break inside it.

Use Array#some

Instead, use Array#some:

[1, 2, 3].some(function(el) {
  console.log(el);
  return el === 2;
});

This works because some returns true as soon as any of the callbacks, executed in array order, return true, short-circuiting the execution of the rest.

some, its inverse every (which will stop on a return false), and forEach are all ECMAScript Fifth Edition methods which will need to be added to the Array.prototype on browsers where they're missing.

Answer from bobince on Stack Overflow
Top answer
1 of 16
2926

There's no built-in ability to break in forEach. To interrupt execution you would have to throw an exception of some sort. eg.

var BreakException = {};

try {
  [1, 2, 3].forEach(function(el) {
    console.log(el);
    if (el === 2) throw BreakException;
  });
} catch (e) {
  if (e !== BreakException) throw e;
}

JavaScript exceptions aren't terribly pretty. A traditional for loop might be more appropriate if you really need to break inside it.

Use Array#some

Instead, use Array#some:

[1, 2, 3].some(function(el) {
  console.log(el);
  return el === 2;
});

This works because some returns true as soon as any of the callbacks, executed in array order, return true, short-circuiting the execution of the rest.

some, its inverse every (which will stop on a return false), and forEach are all ECMAScript Fifth Edition methods which will need to be added to the Array.prototype on browsers where they're missing.

2 of 16
884

There is now an even better way to do this in ECMAScript2015 (aka ES6) using the new for of loop. For example, this code does not print the array elements after the number 5:

const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (const el of arr) {
  console.log(el);
  if (el === 5) {
    break;
  }
}

From the docs:

Both for...in and for...of statements iterate over something. The main difference between them is in what they iterate over. The for...in statement iterates over the enumerable properties of an object, in original insertion order. The for...of statement iterates over data that iterable object defines to be iterated over.

Need the index in the iteration? You can use Array.entries():

for (const [index, el] of arr.entries()) {
  if ( index === 5 ) break;
}
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › foreach-break
How to Break Out of a JavaScript forEach() Loop - Mastering JS
October 5, 2020 - // Prints "1, 2, 3" [1, 2, 3, 4, 5].every(v => { if (v > 3) { return false; } console.log(v); // Make sure you return true. If you don't return a value, `every()` will stop. return true; }); With every(), return false is equivalent to a break, and ...
Discussions

How do I break out of a function while in a forEach loop?
Others have answered directly, but this is not the best practice. Consider doing the following : const isChecked = [...allHovered].some(node => node.classList.contains("checked")) More on reddit.com
🌐 r/learnjavascript
13
13
August 18, 2022
Is there a way to exit a forEach loop early?
Some people will do literally anything to avoid learning to use simple built-in array methods. This problem would be solved simply and declaratively by using Array.prototype.find(). More on reddit.com
🌐 r/learnjavascript
18
7
August 2, 2019
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › forEach
Array.prototype.forEach() - JavaScript - MDN Web Docs
The forEach() method is generic. It only expects the this value to have a length property and integer-keyed properties. 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.
🌐
Built In
builtin.com › articles › javascript-foreach-break
How to Stop a JavaScript ForEach Loop | Built In
October 7, 2024 - Remember, with great power comes great responsibility. Use the throw Error method wisely, and maybe keep it as your secret JavaScript party trick. Yes, you can break a JavaScript forEach loop by throwing an error.
🌐
DEV Community
dev.to › mayankav › why-you-can-t-break-out-of-the-foreach-loop-n5e
Why can't you break out of the forEach loop? - DEV Community
September 29, 2021 - You can't break from forEach because that's the purpose of forEach loop. In JS you have reduce, map, while, do while, for, filter and god knows how many other types of loops. The reason for forEach to exist is to NOT allow you to break from it!
🌐
Futurestud.io
futurestud.io › tutorials › how-to-exit-stop-or-break-an-array-foreach-loop-in-javascript-or-node-js
How to Exit, Stop, or Break an Array#forEach Loop in JavaScript or Node.js
May 27, 2021 - You may use a for…of loop instead of Array#forEach. You can exit a for…of loop using the break keyword. Then, JavaScript will break out of the loop:
Find elsewhere
🌐
Reddit
reddit.com › r/learnjavascript › is there a way to exit a foreach loop early?
r/learnjavascript on Reddit: Is there a way to exit a forEach loop early?
August 2, 2019 -

One scenario where I choose a for loop over the forEach() method is when I want to break out of a loop early. Imagine I had a longer list of animals and as soon as I found one that matches some criteria, I want to perform some action. If I used forEach(), it would iterate over every single animal resulting in unnecessary iterations, potentially causing performance issues depending on how long the array is. With a for loop, you have the ability to break out early and stop the loop from continuing.

https://davidtang.io/2016/07/30/javascript-for-loop-vs-array-foreach.html

🌐
JavaScript in Plain English
javascript.plainenglish.io › javascript-interview-can-you-stop-or-break-a-foreach-loop-9608ba2a1710
JavaScript Interview: Can You Stop or Break a forEach Loop? 🛑 | by Jayanth babu S | JavaScript in Plain English
September 1, 2024 - JavaScript’s forEach method is a popular tool for iterating over arrays. It executes a provided function once for each array element. However, unlike traditional for or while loops, forEach is designed to execute the function for every element, without a built-in mechanism to stop or break the loop prematurely.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-stop-foreach-method-in-javascript
How to stop forEach() method in JavaScript ? - GeeksforGeeks
July 23, 2025 - Method 2: This method does not actually break from the forEach() loop but thinks of it as a continuous statement against all other elements i.e.
🌐
Medium
medium.com › @anandkiit94 › how-to-break-out-of-a-javascript-foreach-loop-57296857bfcf
How to Break Out of a JavaScript forEach () Loop | by Anand Shankar | Medium
October 1, 2023 - How to Break Out of a JavaScript forEach () Loop As we understand every forEach iteration is a function and we can't just do a break, only return but this won't stop forEach. forEach() function …
🌐
DEV Community
dev.to › codenutt › why-you-can-t-break-a-foreach-loop-bytesize-js-d51
Why you can't break a forEach loop | ByteSize JS - DEV Community
April 8, 2020 - In the case of the forEach() function, it doesn't do anything with the returned code. Be aware, that is not the case for some of the other Array Methods. Additionally, because of this, break or continue are not valid statements.
🌐
TutorialsPoint
tutorialspoint.com › how-to-stop-foreach-method-in-javascript
How to stop forEach() method in JavaScript?
September 12, 2023 - <html> <body> <h2>Using the <i>try-catch statement</i> to stop the execution of the forEach() loop.</h2> <div id = "output"> </div> <script> let output = document.getElementById('output'); let array = ["Cpp", "c", "Java", "JavaScript", 06, true, 43, false]; try { array.forEach((ele) => { if (typeof ele == "number") { throw new Error("Break the loop.") } output.innerHTML += "Element value is " + ele + "<br/>"; }) } catch (error) { output.innerHTML += "Exception thrown from the forEach loop.
🌐
Medium
medium.com › front-end-weekly › 3-things-you-didnt-know-about-the-foreach-loop-in-js-ff02cec465b1
3 things you didn’t know about the forEach loop in JS | by Tiberiu Oprea | Frontend Weekly | Medium
November 24, 2025 - const array = [1, 2, 3, 4];for (let i = 0; i < array.length; i++) { console.log(array[i]); if (array[i] === 2) break; }// Output: 1 2 · Do you expect the code below to skip printing 2 to the console and only show 1 3 4 ? const array = [1, 2, 3, 4];array.forEach(function (element) { if (element === 2) continue; console.log(element); });// Output: Uncaught SyntaxError: Illegal continue statement: no surrounding iteration statement
🌐
Medium
medium.com › @umar.bwn › break-from-foreach-javascript-simplifying-array-iteration-4ece8f6c20b2
Break from foreach JavaScript: Simplifying Array Iteration | by Umar Farooq | Medium
June 23, 2023 - The forEach method in JavaScript was designed to provide a simple and elegant way to iterate over array elements. It intentionally doesn't include a break statement to ensure consistent behavior across different implementations and maintain ...
🌐
W3Schools
w3schools.com › js › js_break.asp
JavaScript Break
The break statement terminates the execution of a loop or a switch statement.
🌐
Byby
byby.dev › js-foreach-break
How to break out of forEach loop in JavaScript
There is no official way to break out of a forEach loop in JavaScript. However, there are some workarounds and alternatives that can achieve the same effect.
🌐
Medium
medium.com › @abdulhannanmahmood › how-to-stop-iteration-of-foreach-loop-in-javascript-949fe564cc90
How to Stop Iteration of forEach Loop in Javascript
March 14, 2023 - In conclusion, there are multiple ways to stop the iteration of a forEach loop in Javascript. You can use the “return” keyword, “break” statement, flag variable, or the “some” method.
🌐
DEV Community
dev.to › mayallo › how-to-break-from-foreach-in-javascript-24mm
How to Break from forEach in JavaScript? - DEV Community
June 26, 2023 - There is no way to stop or break a forEach() loop other than by throwing an exception.