Skip to content

arrayLoops

Reports using .forEach() when a for-of loop can be used.

✅ This rule is included in the ts stylistic and stylisticStrict presets.

for...of loops offer several benefits over the .forEach() method:

  • Ability to exit early with break or return
  • Ability to skip iterations with continue
  • TypeScript type narrowing works properly since no function boundary is crossed
  • Often better performance

This rule reports when .forEach() is called on arrays.

AEdeclare consB0arr7<2:sp ansty="le--0:#C792EA;--1:#8844">AB0numb53[<];d ivclassNa="meec-li">53arrAB0forEa5<3(sp ansty="le--0:#D9F5DD;--1:#1111">1<1(sp ansty="le--0:#D7DBE0;--1:#403F">53eleme1<1)sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#C792EA;--1:#8844">AE<=>sp ansty="le--0:#D6DEEB;--1:#403F">53sp>< {d ivclassNa="meec-li">53< sp ansty="le--0:#D6DEEB;--1:#403F">53consoAB0l53(element<);d ivclassNa="meec-li">53}<);d ivclassNa="meco"><);d>AEdeclare consB0arr7<2:sp ansty="le--0:#C792EA;--1:#8844">AB0stri53[<];d ivclassNa="meec-li">53arrAB0forEa5<3(sp ansty="le--0:#C792EA;--1:#8844">AEfuncti5<3 sp ansty="le--0:#D9F5DD;--1:#1111">1<1(sp ansty="le--0:#D7DBE0;--1:#403F">53it1<1)sp ansty="le--0:#D6DEEB;--1:#403F">53< {d ivclassNa="meec-li">nt< sp ansty="le--0:#82AAFF;--1:#3B61">B0proce53(item<);d ivclassNa="meec-li">53}<);d ivclassNa="meco"><);d>5<3[sp ansty="le--0:#F78C6C;--1:#AA09">8<21sp ansty="le--0:#D6DEEB;--1:#403F">53<, sp ansty="le--0:#F78C6C;--1:#AA09">8<22sp ansty="le--0:#D6DEEB;--1:#403F">53<, sp ansty="le--0:#F78C6C;--1:#AA09">8<23sp ansty="le--0:#D6DEEB;--1:#403F">5<3]sp ansty="le--0:#C792EA;--1:#8844">AB0forEa5<3(sp ansty="le--0:#D9F5DD;--1:#1111">1<1(sp ansty="le--0:#D7DBE0;--1:#403F">53val1<1)sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#C792EA;--1:#8844">AE<=>sp ansty="le--0:#D6DEEB;--1:#403F">53sp>< {d ivclassNa="meec-li">nt< sp ansty="le--0:#82AAFF;--1:#3B61">B0hand53(value<);d ivclassNa="meec-li">53}<);d ivclassNa="meco"><);d>
44AEdeclare co61B0a64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0nu3F5<3[] ;divclass="Nameec-">44A3F<53 ( spans="tyle--0:#C792EA;--1:#88">44AEco61B0ele4<4AE spans="tyle--0:#7FDBCA;--1:#09">6E<72of spans="tyle--0:#D6DEEB;--1:#40">3F53 arra3F<53 spans="tyle--0:#D6DEEB;--1:#40">3F53con4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B<0log spans="tyle--0:#D6DEEB;--1:#40">3F53(eleme3<);><}di vdivclass="Nameexpressive-">44AEdeclare co61B0a64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0st3F5<3[] ;divclass="Nameec-">44A3F<53 ( spans="tyle--0:#C792EA;--1:#88">44AEco61B04<4AE spans="tyle--0:#7FDBCA;--1:#09">6E<72of spans="tyle--0:#D6DEEB;--1:#40">3F53 arrade61B0pro3F53(it3<);><}di vdivclass="Nameexpressive-">44AEdeclare co61B0a64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0nu3F5<3[] ;divclass="Nameec-">3F53a4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B<0map spans="tyle--0:#D6DEEB;--1:#40">31<111( spans="tyle--0:#D7DBE0;--1:#40">3F53ele1<111) spans="tyle--0:#D6DEEB;--1:#40">344 spans="tyle--0:#D6DEEB;--1:#40">3F53 elem>4<4AE* spans="tyle--0:#D6DEEB;--1:#40">30<9822 spans="tyle--0:#D6DEEB;--1:#40">3F<53) ;divclass="Name">< 2)>

This rule is not configurable.

If you prefer the functional style of .forEach(), or if your codebase uses it extensively for consistency, you may disable this rule. Note that this rule intentionally does not flag .forEach() on Set or Map since those cannot be as naturally replaced with for-of in all cases.