How to break nested for loop using JavaScript?
The break statement, which is used to exit a loop early.
A label can be used with a break to control the flow more precisely. A label is simply an identifier followed by a colon(:) that is applied to a statement or a block of code.
Note: there should not be any other statement in between a label name and associated loop.
Example-1: Break from nested loop
<!DOCTYPE html> <html> <head> <title> Break Nested For loop </title> </head> <body> <script type="text/javascript"> <!-- document.write( "Entering the Geeks For Geeks!<br /> "); for (var i = 0; i < 5; i++) { document.write( "For Upper Level in GfG : " + i + "<br />"); document.write("<br />") for (var j = 0; j < 5; j++) { // Break from the inner loop if (j == 3) break; document.write( "For Deeper Level in GfG : " + j + " <br />"); } // Break from the outer loop if (i == 3) break; } document.write("Exiting the Geeks For Geeks!<br /> "); </script> </body> </html |
Output:
Entering the Geeks For Geeks! For Upper Level in GfG : 0 For Deeper Level in GfG : 0 For Deeper Level in GfG : 1 For Deeper Level in GfG : 2 For Upper Level in GfG : 1 For Deeper Level in GfG : 0 For Deeper Level in GfG : 1 For Deeper Level in GfG : 2 For Upper Level in GfG : 2 For Deeper Level in GfG : 0 For Deeper Level in GfG : 1 For Deeper Level in GfG : 2 For Upper Level in GfG : 3 For Deeper Level in GfG : 0 For Deeper Level in GfG : 1 For Deeper Level in GfG : 2 Exiting the Geeks For Geeks!
Example-2: Break from nested loop using Labels.
<!DOCTYPE html> <html> <head> <title> Break Nested For loop Using Labels </title> </head> <body> <script type="text/javascript"> <!-- document.write("Entering the Geeks for Geeks!<br /> "); upperloop: // This is the label name for (var i = 0; i < 5; i++) { document.write( "For Upper Level in GfG : " + i + "<br />"); document.write("<br />"); deeperloop: for (var j = 0; j < 5; j++) { // Break from the inner loop if (j > 3) break; // Do the same thing if (i == 2) break deeperloop; // Break from the outer loop if (i == 3) break upperloop; document.write("For Deeper Level in GfG: " + j + " <br />"); } } document.write("Exiting the Geeks For Geeks!<br /> "); </script> </body> </html> |
Output:
Entering the Geeks for Geeks! For Upper Level in GfG : 0 For Deeper Level in GfG: 0 For Deeper Level in GfG: 1 For Deeper Level in GfG: 2 For Deeper Level in GfG: 3 For Upper Level in GfG : 1 For Deeper Level in GfG: 0 For Deeper Level in GfG: 1 For Deeper Level in GfG: 2 For Deeper Level in GfG: 3 For Upper Level in GfG : 2 For Upper Level in GfG : 3 Exiting the Geeks For Geeks!
Recommended Posts:
- PHP break (Single and Nested Loops)
- JavaScript | Break and Continue
- JavaScript | For Loop
- JavaScript | While Loop
- How to use loop through an array in JavaScript?
- What is the difference between “word-break: break-all” versus “word-wrap: break-word” in CSS ?
- CSS | page-break-after Property
- CSS | word-break Property
- How to break line without using <br> tag in HTML / CSS ?
- CSS | box decoration break Property
- CSS page-break-before Property
- CSS | page-break-inside Property
- How to prevent column break within an element?
- break command in Linux with examples
- Tips for restarting career after a break
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



