Skip to content

Files

Latest commit

 

History

History
25 lines (19 loc) · 521 Bytes

guard-for-in.md

File metadata and controls

25 lines (19 loc) · 521 Bytes

Pattern: Unguarded for-in loop

Issue: -

Description

A for-in loop iterates over all enumerable properties of an object, including those inherited through the prototype chain. Without filtering the results with an if statement, unexpected behavior can arise from inherited properties.

Examples

Example of incorrect code:

for (key in foo) {
  doSomething(key);
}

Example of correct code:

for (key in foo) {
  if (Object.hasOwn(foo, key)) {
    doSomething(key);
  }
}