Pattern: Use of Array#forEach
Issue: -
Using for...of
loops instead of Array#forEach()
provides better performance, readability, and control flow (ability to use break
or return
). It also enables better type-narrowing in TypeScript without crossing function boundaries.
Example of incorrect code:
const foo = [1, 2, 3];
foo.forEach((element) => {
/* ... */
});
Example of correct code:
const foo = [1, 2, 3];
for (const element of foo) {
/* ... */
}