Search code examples
jslintjshinthasownproperty

jshint no error for forin (hasOwnProperty)


Why jshint is not reporting forin (hasOwnProperty) error for the following code? jslint do report error on it but jshint doesn't.

/*jshint forin: true */

(function () {
    "use strict";

    var obj = {a: 1, b: 2}, i = null;

    for (i in obj) {
        if (i === 0) {
            console.log('blah...');
        }
    }
}());

Solution

  • Here's the relevant snippet of code from JSHint (modified slightly for formatting):

    if (
        state.option.forin && 
        s && 
        (s.length > 1 || typeof s[0] !== "object" || s[0].value !== "if")
    ) {
        warning("W089", this);
    }
    

    The important part is s[0].value !== "if". JSHint won't raise an error if the first statement of the for...in body is an if statement, regardless of the condition of that statement.