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.