1 # Require Guarding for-in (guard-for-in)
3 Looping over objects with a `for in` loop will include properties that are inherited through the prototype chain. This behavior can lead to unexpected items in your for loop.
11 Note that simply checking `foo.hasOwnProperty(key)` is likely to cause an error in some cases; see [no-prototype-builtins](no-prototype-builtins.md).
15 This rule is aimed at preventing unexpected behavior that could arise from using a `for in` loop without filtering the results in the loop. As such, it will warn when `for in` loops do not filter their results with an `if` statement.
17 Examples of **incorrect** code for this rule:
20 /*eslint guard-for-in: "error"*/
27 Examples of **correct** code for this rule:
30 /*eslint guard-for-in: "error"*/
33 if (Object.prototype.hasOwnProperty.call(foo, key)) {
39 if ({}.hasOwnProperty.call(foo, key)) {
47 * [no-prototype-builtins](no-prototype-builtins.md)
51 * [Exploring JavaScript for-in loops](https://javascriptweblog.wordpress.com/2011/01/04/exploring-javascript-for-in-loops/)
52 * [The pitfalls of using objects as maps in JavaScript](http://2ality.com/2012/01/objects-as-maps.html)