coolguy
coolguy

Reputation: 7954

Use of break and continue in jQuery

I know with core Javascript we can do things like this:

for(i=1;i<10;i++){
   if(i==5){
    //break or continue
   }
}

But why this is not correct ?

$.each(iteratorarray,function(i){ //iteratorarray - just an array
  if(i==5){
    //break or continue,will cause error here
  }
});

Upvotes: 6

Views: 15669

Answers (6)

Bergi
Bergi

Reputation: 664548

why this is not correct?

break or continue statements are only valid as a part of a (for-, for-in-, while-, …) loop statement. You cannot do that in a plain function. From the spec:

A program is considered syntactically incorrect if […] the program contains a continue/break statement […] directly or indirectly (but not crossing function boundaries), within an IterationStatement.

Instead, you can return from a function (which breaks further execution of the function body). As you can read in the documentation, returning false stops the iteration in $.each.

Upvotes: 2

Arun P Johny
Arun P Johny

Reputation: 388316

$.each uses a callback to execute the custom code, break is a control statement which can disrupt the wrapping loop. The break or continue has to be call directly from the loop body not from a delegated function.

$.each(iteratorarray,function(i){ //iteratorarray - just an array
  if(i==5){
    //break or continue,will cause error here
  }
});

Can be considered as equals to

for(i=0;i<iteratorarray.length;i++){
    if(callback(i, iteratorarray[i]) === false){
        break;
    }
}

Here your code is residing inside the callback() function therefore you cannot use loop control statements inside the callback.

You can read more in the documentation.

Upvotes: 2

Arvind Bhardwaj
Arvind Bhardwaj

Reputation: 5291

You can also use JavaScript's every method for iterating array elements.

The every method calls the callbackfn function one time for each array element, in ascending index order, until the callbackfn function returns false. If an element that causes callbackfn to return false is found, the every method immediately returns false. Otherwise, the every method returns true.

Upvotes: 1

AI Generated Response
AI Generated Response

Reputation: 8835

In the $.each case, there is no loop you can break out of, and that's why you get the error. The way jQuery does it, however, is that the loop breaks if your function returns false.

Upvotes: 1

Adil
Adil

Reputation: 148120

This behavior is what is implemented by jQuery. This is what jQuery api document say "We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration", You can read about it here.

Upvotes: 9

Explosion Pills
Explosion Pills

Reputation: 191749

return will continue, return false will break. The documentation talks about the break substitute by example, but it still specifies the functionality of return in the .each callback.

Upvotes: 4

Related Questions