Rule: forin
Requires a for ... in
statement to be filtered with an if
statement.
Rationale
for (let key in someObject) {
if (someObject.hasOwnProperty(key)) {
// code here
}
}
Prevents accidental iteration over properties inherited from an object’s prototype.
See MDN’s for...in
documentation for more information about for...in
loops.
Also consider using a Map
or Set
if you’re storing collections of objects.
Using Object
s can cause occasional edge case bugs, such as if a key is named “hasOwnProperty”.
Config
Not configurable.
Config examples
"forin": true
Schema
null