10

This is theoretical question to understand how many escapes (return or exit) can apply to nested loops or other controls and functions.

I confused about this because I am stuck in the code How to escape from for ... each loop and method at the same time?

I can't stop iterating over options in select element.

I tried return and return false already, but am unsuccesful.

Generally how we can do that?

function() {
    for (...) {
        if (...) {
            $(...).each(function() {
               // You have to exit outer function from here
            });
        }
    }
}
Community
  • 1
  • 1
caglaror
  • 459
  • 1
  • 13
  • 28

6 Answers6

10

Use a shared variable between the loops. Flip it to true at the end of the each() loop if you want to exit and at the end of the for-loop check for it being true. If yes, break out of it.

Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
4

I would do it this way:

  • Create a boolean variable to check on each loop, and if the variable is true, then exit the loop (do this for each).

    var exitLoop = false;
    
     $(sentences).each(function() {
        if(exitLoop) {return;}
        var s = this;
        alert(s);
        $(words).each(function(i) {
        if(exitLoop) {return;}
            if (s.indexOf(this) > -1)
            {
                alert('found ' + this);
                throw "Exit Error";
            }
        });
    });
    

Note this is not the correct use of a try-catch as a try-catch should strictly be used for error handling, not jumping to different sections of your code - but it will work for what you're doing.

If return is not doing it for you, try using a try-catch

try{
$(sentences).each(function() {
    var s = this;
    alert(s);
    $(words).each(function(i) {
        if (s.indexOf(this) > -1)
        {
            alert('found ' + this);
            throw "Exit Error";
        }
    });
});
}
catch (e)
{
    alert(e)
}

Code taken from this answer

Community
  • 1
  • 1
What have you tried
  • 11,018
  • 4
  • 31
  • 45
1

The loop can also be exited by changing the iterator value.

var arr = [1,2,3,4,5,6,7,8,9,10];

for(var i = 0;i<arr.length;i++){

  console.log(i);
  compute(i);

  function compute(num){
    //break is illegal here
    //return ends only compute function
    if(num>=3) i=arr.length;
  }

}
Paweł
  • 4,238
  • 4
  • 21
  • 40
0

"label"s are the solution for scope processes like breaking loops. Here is the built-in answer I realized after years: https://css-tricks.com/you-can-label-a-javascript-if-statement/

caglaror
  • 459
  • 1
  • 13
  • 28
0

Not ideal but you can do

let result = false;
function() {
    for (...) {
        if (...) {
            $(...).each(function() {
                result = true;
               
            });
        }
    }
    if (result == true){
        return;
    }

}
-1

As in most languages. The keyword to exit a loop is break; More info here: http://www.w3schools.com/js/js_break.asp

Toon Casteele
  • 2,479
  • 15
  • 25