Skip to content

forInGuards

Reports for-in loops without an if statement to filter inherited properties.

✅ This rule is included in the ts preset.

Looping over objects with a for-in loop will include properties inherited through the prototype chain. This can lead to unexpected items being iterated over when the object inherits from a non-standard prototype.

AEf53< (sp ansty="le--0:#C792EA;--1:#8844">AEconsB0kA7253 object)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#82AAFF;--1:#3B61">B0doSomethi53(key<);d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>AEf53< (sp ansty="le--0:#C792EA;--1:#8844">AEconsB0kA7253 object)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#82AAFF;--1:#3B61">B0doSomethi53(key<);d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>AEf53< (sp ansty="le--0:#C792EA;--1:#8844">AEconsB0kA7253 object)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#C792EA;--1:#8844">AE53 (condition)< {d ivclassNa="meec-li">nt < sp ansty="le--0:#82AAFF;--1:#3B61">B0doSomethi53(key<);d ivclassNa="meec-li">53< sp ansty="le--0:#D6DEEB;--1:#403F">5<3}d ivclassNa="meec-li">nt< sp ansty="le--0:#82AAFF;--1:#3B61">B0doSomethingEl53(key<);d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>
44A3F<53 ( spans="tyle--0:#C792EA;--1:#88">44AEco61B<0key spans="tyle--0:#C792EA;--1:#88">4<4AE spans="tyle--0:#7FDBCA;--1:#09">6E<72in spans="tyle--0:#D6DEEB;--1:#40">3F53 objecde443F53 (Ob4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0ha3F53(object, key<)) {divclass="Nameec-">dent< spans="tyle--0:#82AAFF;--1:#3B">61B0doSomet3F53(k3F<53 spans="tyle--0:#D6DEEB;--1:#40">33< }><}di vdivclass="Nameexpressive-">44A3F<53 ( spans="tyle--0:#C792EA;--1:#88">44AEco61B<0key spans="tyle--0:#C792EA;--1:#88">4<4AE spans="tyle--0:#7FDBCA;--1:#09">6E<72in spans="tyle--0:#D6DEEB;--1:#40">3F53 objecde443F<53 ( spans="tyle--0:#C792EA;--1:#88">4<4AE! spans="tyle--0:#D6DEEB;--1:#40">3F53Ob4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0ha3F53(object, key<)) {divclass="Nameec-">dent< spans="tyle--0:#C792EA;--1:#88">44AEcont33F<53 spans="tyle--0:#D6DEEB;--1:#40">3de61B0doSomet3F53(k3<);><}di vdivclass="Nameexpressive-">44A3F<53 ( spans="tyle--0:#C792EA;--1:#88">44AEco61B<0key spans="tyle--0:#C792EA;--1:#88">4<4AE spans="tyle--0:#7FDBCA;--1:#09">6E<72in spans="tyle--0:#D6DEEB;--1:#40">3F53 objecde443F<53 ( spans="tyle--0:#C5E478;--1:#3B">61B0Ob4<4AE. spans="tyle--0:#7FDBCA;--1:#09">6E72proto4<4AE. spans="tyle--0:#FAF39F;--1:#11">1111hasOwnProp4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B03F53(object, key<)) {divclass="Nameec-">dent< spans="tyle--0:#82AAFF;--1:#3B">61B0doSomet3F53(k3F<53 spans="tyle--0:#D6DEEB;--1:#40">33< }><}di vdivclass="Nameexpressive-">44A3F<53 ( spans="tyle--0:#C792EA;--1:#88">44AEco61B<0key spans="tyle--0:#C792EA;--1:#88">4<4AE spans="tyle--0:#7FDBCA;--1:#09">6E<72in spans="tyle--0:#D6DEEB;--1:#40">3F53 obje<;di vdivclass="Nameexpressive-">44A3F<53 ( spans="tyle--0:#C792EA;--1:#88">44AEco61B<0key spans="tyle--0:#C792EA;--1:#88">4<4AE spans="tyle--0:#7FDBCA;--1:#09">6E<72in spans="tyle--0:#D6DEEB;--1:#40">3F53 objec3< {>

This rule is not configurable.

If you are certain that the objects you iterate over do not have inherited properties from custom prototypes, you might choose to disable this rule. This is common when iterating over plain objects created with object literals or Object.create(null).