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.