651

I have some code:

$(xml).find("strengths").each(function() {
   //Code
   //How can i escape from this block based on a condition.
});

How can i escape from the "each" code block based on a condition?

Update:

What if we have something like this:

$(xml).find("strengths").each(function() {
   $(this).each(function() {
       //I want to break out from both each loops at the same time.
   });
});

Is it possible to break out from both "each" functions from the inner "each" function?

# 19.03.2013

If you want to continue instead of break out

return true;
Dexygen
  • 12,287
  • 13
  • 80
  • 147
Orson
  • 14,981
  • 11
  • 56
  • 70

4 Answers4

1078

According to the documentation you can simply return false; to break:

$(xml).find("strengths").each(function() {

    if (iWantToBreak)
        return false;
});
Poutrathor
  • 1,990
  • 2
  • 20
  • 44
Greg
  • 316,276
  • 54
  • 369
  • 333
  • 14
    simple 'return' is not working for me. this is why i googled it and found this post. 'return false' works. i am running osx lion with chrome. – Sonic Soul Jun 09 '12 at 23:17
  • 1
    "simple return" actually returns. It will return control from the `each` and everything else around it and after it until it exit's `each()`'s parent control - such as a function() which is using each. This is the correct answer, and the other approach will not work. – Travis J Aug 24 '12 at 17:35
  • 30
    "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." – jave.web Aug 19 '13 at 13:08
  • 7
    Notice that this will not KILL the main function, the loop can break, but when you use this jQuery each to check for required fields before continue, it will continue no matter the break. for that you should use a var kill variable set in the loop, and read outside of it to return if kill==1 – Miguel Dec 29 '14 at 19:04
135

You can use return false;

+----------------------------------------+
| JavaScript              | PHP          |
+-------------------------+--------------+
|                         |              |
| return false;           | break;       |
|                         |              |
| return true; or return; | continue;    |
+-------------------------+--------------+
aksu
  • 5,221
  • 5
  • 24
  • 39
Subodh Ghulaxe
  • 18,333
  • 14
  • 83
  • 102
  • 1
    I don't understand what you mean – GusDeCooL Apr 04 '13 at 18:44
  • 4
    I mean to say return false; in javascript is same as break in PHP and return true; or return; is same as continue – Subodh Ghulaxe Apr 05 '13 at 07:19
  • 3
    @SubodhGhulaxe it is jQuery not javascript – Codler Apr 08 '13 at 06:56
  • so Codler what is the difference between return false; in javascript and return false; in jQuery – Subodh Ghulaxe Apr 08 '13 at 07:42
  • 6
    @SubodhGhulaxe `return false` in javascript is meaning returning boolean type same in PHP. But in `jquery.each()` it will be interpreted by jQuery as stop the looping. – GusDeCooL Apr 08 '13 at 13:23
  • Returning `false` to jQuery's `.each()` is not the same as `break` in PHP. You have the right idea, but this is not really accurate. Plus, this question had a correct answer over 3 years ago. – Brad May 13 '13 at 21:20
  • You are mixing your concepts here, can't tell if you meant to write "JQuery" where you have "JavaScript" – Kris Boyd Feb 17 '16 at 17:49
110

Return false from the anonymous function:

$(xml).find("strengths").each(function() {
  // Code
  // To escape from this block based on a condition:
  if (something) return false;
});

From the documentation of the each method:

Returning 'false' from within the each function completely stops the loop through all of the elements (this is like using a 'break' with a normal loop). Returning 'true' from within the loop skips to the next iteration (this is like using a 'continue' with a normal loop).

RRikesh
  • 14,112
  • 5
  • 49
  • 70
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
28
if (condition){ // where condition evaluates to true 
    return false
}

see similar question asked 3 days ago.

Community
  • 1
  • 1
adardesign
  • 33,973
  • 15
  • 62
  • 84