How to break or continue across nested for each loops in TypeScript?

Sometimes, we want to break or continue across nested for each loops in TypeScript.

In this article, we’ll look at how to break or continue across nested for each loops in TypeScript.

How to break or continue across nested for each loops in TypeScript?

To break or continue across nested for each loops 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.

We can also use continue in a for-of loop to skip to the next iteration.

Conclusion

To break or continue across nested for each loops in TypeScript, we replace forEach with a for-of loop.