7

I have always believed you use continue as follows:

var i;

for (i = 0;  i < 10; i++) {
  if(i%2===0) {
    continue;
  }
}   

Or

var i, myloop;

myloop: for (i = 0;  i < 10; i++) {
  if(i%2===0) {
    continue myloop;
  }
}

But when I run these two snippets of code through JSLint, I get the error:


Problem at line 5 character 5: Unexpected 'continue'.
     continue;

What am I doing wrong? What is the correct usage?

2
  • 2
    You had to move the var i up to even get that far, right? Improving your code is JSLint's secondary concern, enforcing Doug Crockford's personal opinions about JavaScript being its first. There's always JSHint if you want a less opinionated alternative. May 4, 2011 at 15:48
  • 1
    Yes. JSHint. JSLint is far too dogmatic. Sep 29, 2012 at 9:18

5 Answers 5

9

I guess JSLint just hates continue:

There are almost always better ways of writing statements that more explicitly define what you are attempting to do without resorting to continue. JSLint is all about the good parts, and not about the parts that are acceptable. It forces you to use a higher standard than the one defined.

Douglas says it best in his book:

"The continue statement jumps to the top of the loop. I have never seen a piece of code that was not improved by refactoring it to remove the continue statement."

6
  • 10
    That's so stupid though. 'continue' makes it easy to 'exit early' in a loop. I guess the inner loop can be placed in an external function, to keep readability, but that's not always handy.
    – Evert
    May 4, 2011 at 15:38
  • @Evert: You must be confusing continue with break (which does what you say and which is not frown upon by Crockford).
    – xofon
    May 4, 2011 at 16:35
  • 1
    No, sorry.. say you want to loop an array but only do something with certain elements in that array. I can do if (!condition) continue; at the top of the loop, or if (condition) { .. } stuff. The former has the advantage that there's one less level of nesting.
    – Evert
    May 5, 2011 at 7:48
  • @Evert: I see, now I understand your wording ('exit early in a loop' had confused me). As concerns the question of continue vs. nesting, the point is that having that extra level of nesting is a Good Thing, since it gives a clear visual clue that the code applies only when the condition is satisfied. (Personally, I'm not sure whether I agree more with Crockford or with you, but I'd definitely not call it 'so stupid'.)
    – xofon
    May 6, 2011 at 16:42
  • 16
    I've never understood what's so hard to read about continue. The second you've established you no longer need to do anything else on a given iteration there's no reason not to use it. Mar 8, 2012 at 23:33
3

You must tick "Tolerate continue", Crockford doesn't like it.

0

There is an option at the bottom of JSlint that says

Tolerate continue

maybe it has issues with it?

0

JSLint doesn't like continue because Douglas doesn't like it.

continue skips to the next iteration. If you don't insert it, the code will carry on executing. If you want to exit the loop, use break.

0

This was good enough for me. // eslint-disable-next-line no-continue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.