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 forEach loop in TypeScript?

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

For instance, we write

for (const a of ratings) {
  if (somethingWrong) {
    break;
  }
  //...
}

to loop through the ratings array.

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

Conclusion

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

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *