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">A2:sp>B0numb53[<];d ivclassNa="meec-li">53arrAB0forEa5<3(sp ansty="le--0:#D9F5DD;--1:#1111">13(sp><1(sp ansty="le--0:#D7DBE0;--1:#403F">53eleme1(sp>1<1)sp ansty="le--0:#D6DEEB;--1:#403F">51)sp><3 sp ansty="le--0:#C792EA;--1:#8844">AE3 sp><=>sp ansty="le--0:#D6DEEB;--1:#403F">53=>sp>];d>< {d ivclassNa="meec-li">53< sp ansty="le--0:#D6DEEB;--1:#403F">53conso sp>AB0l53(element {d><);d ivclassNa="meec-li">53});d><);d ivclassNa="meco"> { console.log(element);}"><);d>);d>);d>AEdeclare consB0arr7<2:sp ansty="le--0:#C792EA;--1:#8844">A2:sp>B0stri53[<];d ivclassNa="meec-li">53arrAB0forEa5<3(sp ansty="le--0:#C792EA;--1:#8844">AEfuncti3(sp>5<3 sp ansty="le--0:#D9F5DD;--1:#1111">13 sp><1(sp ansty="le--0:#D7DBE0;--1:#403F">53it1(sp>1<1)sp ansty="le--0:#D6DEEB;--1:#403F">531)sp>];d>< {d ivclassNa="meec-li">nt< sp ansty="le--0:#82AAFF;--1:#3B61">B0proce sp>53(item {d><);d ivclassNa="meec-li">53});d><);d ivclassNa="meco"><);d>);d>);d>5<3[sp ansty="le--0:#F78C6C;--1:#AA09">83[sp><21sp ansty="le--0:#D6DEEB;--1:#403F">5321sp><, sp ansty="le--0:#F78C6C;--1:#AA09">8, sp><22sp ansty="le--0:#D6DEEB;--1:#403F">5322sp><, sp ansty="le--0:#F78C6C;--1:#AA09">8, sp><23sp ansty="le--0:#D6DEEB;--1:#403F">523sp><3]sp ansty="le--0:#C792EA;--1:#8844">A3]sp>B0forEa5<3(sp ansty="le--0:#D9F5DD;--1:#1111">13(sp><1(sp ansty="le--0:#D7DBE0;--1:#403F">53val1(sp>1<1)sp ansty="le--0:#D6DEEB;--1:#403F">51)sp><3 sp ansty="le--0:#C792EA;--1:#8844">AE3 sp><=>sp ansty="le--0:#D6DEEB;--1:#403F">53=>sp>< {d ivclassNa="meec-li">nt< sp ansty="le--0:#82AAFF;--1:#3B61">B0hand sp>53(value {d><);d ivclassNa="meec-li">53});d><);d ivclassNa="meco"> { handle(value);}"><);d>);d>);d> 44AEdeclare co61B0a64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0nu4AE >3F5<3[] ;divclass="Nameec-">44A3F<53 ( spans="tyle--0:#C792EA;--1:#88">44AEco53 (>61B0ele4<4AE spans="tyle--0:#7FDBCA;--1:#09">6E4AE ><72of spans="tyle--0:#D6DEEB;--1:#40">3F53 arra72of>3[]>3F<53 spans="tyle--0:#D6DEEB;--1:#40">3F53con53 >4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B4AE.><0log spans="tyle--0:#D6DEEB;--1:#40">3F53(eleme0log>3<);>);><}di vdivclass="Nameexpressive-">44AEdeclare co61B0a64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0st4AE >3F5<3[] ;divclass="Nameec-">44A3F<53 ( spans="tyle--0:#C792EA;--1:#88">44AEco53 (>61B0- 4
<4AE spans="tyle--0:#7FDBCA;--1:#09">6E4AE ><72of spans="tyle--0:#D6DEEB;--1:#40">3F53 arra72of>3[]>de61B0pro3F53(it3<);>);>}di><}di vdivclass="Nameexpressive-">44AEdeclare co61B0a64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0nu4AE >3F5<3[] ;divclass="Nameec-">3F53a4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B4AE.><0map spans="tyle--0:#D6DEEB;--1:#40">30map>1<111( spans="tyle--0:#D7DBE0;--1:#40">3F53ele111(>1<111) spans="tyle--0:#D6DEEB;--1:#40">3111)>44 spans="tyle--0:#D6DEEB;--1:#40">3F53 elem>4<4AE* spans="tyle--0:#D6DEEB;--1:#40">34AE*>0<9822 spans="tyle--0:#D6DEEB;--1:#40">3F9822>3[]><53) ;divclass="Name"> element *">< 2)> 2)>53)>}di>
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.