47

I have a for loop that I want to exit like this:

function MyFunction() {
  for (var i = 0; i < SomeCondition; i++) {
     if (i === SomeOtherCondition) {
        // Do some work here.
        return false;
     }
  }
  // Execute the following code after breaking out of the for loop above.
  SomeOtherFunction();
}

The problem is that after the // Do some work here. statements executes, I want to exit the for loop but still want to execute the code below the whole for loop (everything below // Execute the following code after breaking out of the for loop above.).

The return false statement does exit the for loop but it also exits the whole function. How do I fix this?

2
  • 2
    possible duplicate of How to stop a for loop? Commented May 6, 2012 at 14:56
  • 1
    This is the sort of thing you'll learn in the early chapters of a basic JavaScript tutorial. You may want to consider something like eloquentjavascript.net in order to learn the language basics. Its second chapter teaches the break statement. Commented May 6, 2012 at 15:08

6 Answers 6

92

You're looking for the break statement.

Sign up to request clarification or add additional context in comments.

1 Comment

why are you referring to a C# example?
13

Either use a break or continue statement

function MyFunction() { 
  for (var i = 0; i < SomeCondition; i++) { 

     if (i === SomeOtherCondition) { 

        // Do some work here 
        break;
     } 
  } 

  SomeOtherFunction(); 
  SomeOtherFunction2(); 
} 

Or to continue processing items except for those in a condition

function MyFunction() { 
  for (var i = 0; i < SomeCondition; i++) { 

     if (i != SomeOtherCondition) continue;

     // Do some work here 
  } 

  SomeOtherFunction(); 
  SomeOtherFunction2(); 
} 

Comments

7

Several people have offered break as the solution, and it is indeed the best answer to the question.

However, just for completeness, I feel I should also add that the question could be answered while retaining the return statement, by wrapping the contents of the if() condition in a closure function:

function MyFunction() {

  for (var i = 0; i < SomeCondition; i++) {

     if (i === SomeOtherCondition) {
        function() {
           // Do some work here
           return false;
        }();
     }
  }

  SomeOtherFunction();
  SomeOtherFunction2();
}

As I say, break is probably a better solution in this case, as it's the direct answer to the question and the closure does introduce some additional factors (such as changing the value of this, limiting the scope of variables introduced inside the function, etc). But it's worth offering as a solution, because it's a valuable technique to learn, if not necessarily to be used in this particular occasion then certainly for the future.

3 Comments

ok, thanks. My philosophy is to keep my code simple so that it'll be easier to maintain in the future. Upvoted in case others want to take this direction.
@frenchie - you're absolutely right; that's definitely the best philosophy. break is the right answer in this case. Closures are very powerful and have many uses (most jQuery code is built on them), but this case is too trivial to use one.
This mockup does not break the for loop at all.
5

Break - breaks the whole loop. Continue - skips a step in a loop. So it skips the code below continue;

Comments

1

Would setting the i variable to the somecondition value be a good way?

for (var i=0; i<SomeCondition; i++) {

   if (data[i]===true) {
   //do stuff
   i=SomeCondition;
   }
}

Comments

1

OK maybe this is an old topic, but after reading all the answers I'm wondering why is it that nobody suggested using a while loop instead?

I guess in JavaScript you can break a for loop (which you can not do in many other languages, or is considered a bad practice) but I would still use for loops only for the situations where you want to iterate the loop a fixed amount of times.

This would be my suggestion:

function MyFunction() {

  var i = 0,
      breakLoop = false;

  while (i < SomeCondition && !breakLoop) {

     if (i === SomeOtherCondition) {
        breakLoop = true;
     }

     i++;

  }

  SomeOtherFunction();
  SomeOtherFunction2();

}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.