Categories
TypeScript Answers

How to break a forEach loop in TypeScript?

Spread the love

Sometimes, we want to break a forEach loop in TypeScript.

In this article, we’ll look at how to break a forEach loop in TypeScript.

How to break a forEach loop in TypeScript?

To break a forEach loop in TypeScript, we replace forEach with a for-of loop.

For instance, we write

for (const a of this.tab.committee.ratings) {
  if (somethingWrong) {
    break;
  }
}

to loop through the this.tab.committee.ratings iterable object with a for-of loop.

And if somethingWrong is true, then we use break to stop the loop.

Conclusion

To break a forEach loop in TypeScript, we replace forEach with a for-of loop.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.