Search code examples
javascriptfor-loopfor-in-loophasownproperty

Is a simple `continue` statement an acceptable alternative to nesting an entire for..in loop's body in the `if`?


Typically, the solution to for..in's notorious caveat is something along the lines of:

for(var prop in obj) {
    if(obj.hasOwnProperty(prop)) {
        foo(); bar(); baz();
    }
}

I feel like it would be cleaner to just do:

for(var prop in obj) {
    if(!obj.hasOwnProperty(prop)) continue;
}

The question is... Are they not functionally identical?


Solution

  • They are functionally identical. Period.

    As for the matter of style, Douglas and his JSLint say: don't use continue:

    Avoid use of the continue statement. It tends to obscure the control flow of the function.

    See http://javascript.crockford.com/code.html and search for "continue"