Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct use of continue in javascript

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?

like image 903
David Avatar asked May 04 '11 15:05

David


2 Answers

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."

like image 168
sdleihssirhc Avatar answered Oct 20 '22 17:10

sdleihssirhc


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

like image 25
Alex K. Avatar answered Oct 20 '22 17:10

Alex K.