31

I have:

while (i < l) {
   if (one === two) { continue; }
   i++;
}

But JSLint says:

Problem at line 1 character 20: Unexpected 'continue'.

if (one === two) { continue; }

What mistake did I make? How should my code really look?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
asifg
  • 411
  • 2
  • 5
  • 5
  • 7
    I was originally taught to avoid this type of control flow with `continue`. I've decided it was bad advice, and I think it's an option better turned off in JSLint. I think **not* using it obscures intent. If you use a `continue` like you did, it tells me here's a guarded condition to be avoided. Otherwise you reverse the logic and nest everything to be executed under an if. This just adds extra brace and indentation noise in the code. – Tod Apr 16 '13 at 19:46
  • See also: http://stackoverflow.com/questions/16613790/jslint-error-expected-ignore-and-instead-saw-ex – Dave Jarvis Jan 16 '15 at 00:35

2 Answers2

28

From the JSLint docs:

continue Statement

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

So take it out entirely if you want to conform to the conventions that JSLint follows.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 6
    +1 this is a somewhat recent change in JSLint, which used to allow continue before. There is now an option continue:true to tolerate usage of continue. – Eric Bréchemier May 20 '11 at 12:23
  • what to use instead of continue? no return – asifg May 20 '11 at 12:36
  • 2
    I recently updated several projects to comply with this new restriction: typically, you would enclose the remaining code in the loop in an if with the opposite condition. The impact was rather limited in our case, but I tend to find the alternate version using continue more readable and considered adding continue:true to my JSLint config to keep the code as is. – Eric Bréchemier May 20 '11 at 12:44
  • 2
    Generally you'd refactor your code to avoid the use of continue; Douglas Crockford's opinion is that he's "never seen a piece of code that was not improved by refactoring it to remove the continue statement". On the whole, I'd agree, but I have seen some occasional uses where refactoring would subject the code to the law of diminishing returns. It's a judgement call, basically. – Daniel Attfield May 20 '11 at 12:46
  • 2
    some of the rules set by Douglas Crockford really annoy me even thou he knows what he's talking about – Vlad Balmos Oct 24 '11 at 11:08
  • 54
    removing continue often leads to christmas tree code. (nested ifs). not something i like. – Andrew Young Jul 11 '12 at 18:14
  • @AndrewYoung If you're using the `forEach` method of arrays instead of a `for` statement, you can `return` instead of `continue`. – Damian Yerrick Jul 30 '16 at 18:21
  • It'll only obscure the control flow for programmers who can't aren't comfortable understanding what continue does. For those that do it'll clarify the control flow. – Alex Oct 20 '21 at 08:06
14

What JSLint actually tries to say is to invert the if so you can eliminate the continue:

while (i < 1) {
    if (one !== two) {
        i += 1;
    }
}

Furthermore, don't use "i++", but use "i+=1", if you want to stick to the strict guides of JSLint.

Hope this helps :)

Roy Arisse
  • 141
  • 1
  • 2