Bad for-in variable '{a}'

History

This warning has existed in several forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since.

  • In JSLint and JSHint prior to version 1.0.0 the warning has always been "Bad for-in variable '{a}'"

  • In JSHint 1.0.0 and above the warning is "Creating global 'for' variable"

  • In ESLint the warning has always been "Invalid left-hand side in for-in"

The situations that produce the warning have not changed despite changes to the text of the warning itself.

When do I get this error?

The "Bad for-in variable '{a}'" error (and the alternative "Creating global 'for' variable" and "Invalid left-hand side in for-in" errors) are thrown when JSLint, JSHint or ESLint encounters a for-in statement in which the initializer contains a literal value. In the following example we have a for- in statement in which we attempt to assign each property to a string literal:

function test() {
    "use strict";
    var y = {};
    for ("a" in y) {
        // ...
    }
}

JSLint and JSHint also raise this warning when it encounters a for-in statement in which the initializer contains an undefined variable reference. In this example the for-in statement will assign each property to x which has not been declared:

function test() {
    "use strict";
    var y = {};
    for (x in y) {
        // ...
    }
}

Why do I get this error?

In the case of non-identifiers like the string literal in the first example above this error is raised to highlight a fatal reference error. Your code will throw an error in all environments if you do not resolve this issue. The for-in statement effectively assigns a value to its left-hand side expression on each iteration (ES5 §12.6.4). It makes no sense to assign a value to a literal. Imagine it as if you're trying to do 123 = 234.

In the case of an undefined variable this error is raised to highlight a lack of convention and a possible misunderstanding of how the language works. As mentioned previously the for-in statement assigns a value to its left-hand side expression on each iteration. If the variable used in the left-hand side expression has not been declared it will be created as a property of the global object. You can resolve this issue by simply declaring the variable before the for-in statement:

function test() {
    "use strict";
    var y = {},
        x;
    for (x in y) {
        // ...
    }
}

In JSHint 1.0.0 and above you have the ability to ignore any warning with a special option syntax. The identifier of this warning is W088. This means you can tell JSHint to not issue this warning with the /*jshint -W088 */ directive.

In ESLint this error is generated by the Esprima parser and can therefore not be disabled.


About the author

James Allardice

This article was written by James Allardice, Software engineer at Tesco and orangejellyfish in London. Passionate about React, Node and writing clean and maintainable JavaScript. Uses linters (currently ESLint) every day to help achieve this.