JSLint Messages – Strange Loop

An incorrect placement of the ‘break’ statement within a loop will yield this error message. As shown in the following example:

function MyFunc(){
    var x = 0;
    for (x=0; x<10; x++){
        //do something
        break;
    }
}

Here, we will never reach the second iteration of the loop.The ‘break’ statement will kick us out at the end of the first time around. Break statements used in this way should always be wrapped in a conditional statement.

At best this is redundant code that was never cleared up properly, and, at worst, this is some sort of bug that is yet to be identified.

A Guide To JSLint Messages

This article is one of a series on the error and warning messages produced by JSLint.

2 thoughts on “JSLint Messages – Strange Loop

  1. Or you could be exiting the iteration early with continue, and if you get to the end of the loop statement you’re done…

    Granted, it’s easy enough to rewrite in that case.

  2. Pingback: JSLint – A Guide To JSLint Messages | James Wiseman

Leave a Reply