All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Break Foreach Loop In JavaScript

Last Updated : Mar 11, 2024

How To Break Foreach Loop In JavaScript

In this tutorial we will show you the solution of how to break foreach loop in JavaScript, loops are, without a doubt, at the heart of the majority of coding.

Loops come in a variety of shapes and sizes, with a variety of syntaxes. It's difficult to make sense of it all, but knowing what syntax to use is an important element of learning to program, especially when there are several possibilities.

The forEach loop is a JavaScript loop. It's used to loop across arrays. It has a great syntax, but when we try to get a little more involved, such as breaking out of the loop, we run into problems.

Let's look at how to quit a JavaScript forEach loop.

Step By Step Guide On How To Break Foreach Loop In JavaScript :-

A forEach() loop is an array method that executes a callback function for each item in the array. As a result, forEach() can only be used on iterable objects like Arrays, Sets, and Maps.

You don't always want to loop through each and every item in a list. This could be because of the following reasons:

  • You're looking for a specific value.
  • You have a long list and don't require every item on it.

Following is an example of breaking the foreach loop in JavaScript

<html>
<head>
<title></title>
</head>
<body>
<script>
const items = ["one", "two", "three"];
for(const item of items){
  console.log(item);
  if(item == "two" ){
    console.log('found two!');
    break;
  }
}
</style>
</body>
</html>
  1. To start, type <! DOCTYPE html> to tell your browser that the file is in HTML format.
  2. The <html> element, on the other hand, is used to indicate that HTML content is about to begin.
  3. The information about web pages is now contained in the <head> tag. This tag uses the <title> element to provide a web page title. The <head> and <title> tags, for example, are paired tags. As a result, both have the closing tags </head> and </title>.
  4. Finally, the <body> element specifies the content of the web page. This is where all of the website's material will be written. To include our javascript code, we used the script tag within the body element.
  5. The list you wish to iterate through using forEach() is items, and the item inside forEach() represents the single item in the list handed in. You can call it whatever you want, but it's best to use the singular version of your list name
  6. There is no way to cancel a forEach() loop by default. However, you can employ a 'fake' loop break – that is, wrap your logic in a condition so that it only executes when the criteria are met.
  7. Break can, however, be used within a for() loop. When called, break will bring the iteration to a halt.
  8. The < /body> and </html> tags are used to close the body and html tags, respectively.

Conclusion :-

In Javascript, you now know how to quit a forEach loop. You have a number of options as well as the principal workaround.

Remember that code readability is critical, so make your decision carefully. I hope this tutorial on how to break foreach loop in JavaScript helps you.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪