How to Exit a for Loop in JavaScript

  1. Exit the for Loop in JavaScript
  2. Use the break Keyword to Exit for Loop in JavaScript
  3. Use the return Keyword to Exit for Loop in JavaScript
How to Exit a for Loop in JavaScript

The for loop executes code statements repeatedly until the specified condition is met. We need to exit our loop and break the continuous execution most of the time.

In this article, we will learn how to stop and break the execution of the for loop using JavaScript.

Exit the for Loop in JavaScript

We usually use the break and return keywords to stop the for loop execution in JavaScript. We can use those keywords under our desired conditions.

For example, suppose we are looking to find out the special character in an array of data. In that case, we implement the condition equal to the special character and break the execution during traversing the array with the help of a loop.

Use the break Keyword to Exit for Loop in JavaScript

A break can be used in block/braces of the for loop in our defined condition.

Code:

<script>
//break out the execution of for loop if found string
let array = [1,2,3,'a',4,5,6]
for (i = 0; i < array.length; i++) {
  console.log("array value: "+array[i])
    // if the element is string
     if (typeof(array[i])=="string") {
       console.log(array[i]+" is string at index: "+i+", break the loop")
       break; // break the loop
     }
}
</script>

Output:

"array value: 1"
"array value: 2"
"array value: 3"
"array value: a"
"a is string at index: 3, break the loop"

In the above code, we applied a for loop on that array till the length of that array. Inside the loop, we have checked the condition by stating whether our traversed element of an array is a string or not.

If the condition becomes true at any index, we have defined the break statement, and the loop will be stopped. We have printed the log on loop termination to find out the index and element string in the array.

Use the return Keyword to Exit for Loop in JavaScript

We normally use the return statement to stop the execution of functions in programming. If we implement the for loop in the function body, we can stop its execution by using return.

Code:

<script>
let array = [1,2,3,'a',4,5,6]
myFunction();
function myFunction(){
  for (i = 0; i < array.length; i++) {
  console.log("array value: "+array[i])
      // if the element is string
     if (typeof(array[i])=="string") {
      console.log(array[i]+" is string at index: "+i+",loop stoped using return")
       return; // stop the loop using return
    }
  }
}
</script>

Output:

"array value: 1"
"array value: 2"
"array value: 3"
"array value: a"
"a is string at index: 3,loop stoped using return"

We’ve achieved the same result as the first example by using the return keyword. We have placed that for loop code in the function’s body and used the return statement in the body of the if condition.

The return statement will stop the function body and loop execution.

Related Article - JavaScript Loop