How to break nested for loop using JavaScript? Last Updated : 14 May, 2019 Comments Improve Suggest changes Like Article Like Report 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 javascript <!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. javascript <!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! Comment More infoAdvertise with us Next Article How to break nested for loop using JavaScript? R ravikishor Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads Nesting For Loops in JavaScript Nesting for loops is crucial in JavaScript, enabling iteration over multi-dimensional data structures or performing complex tasks. It involves placing one loop inside another, where the outer loop executes for each iteration of the inner loop. This facilitates operations on multi-dimensional arrays 4 min read How to process each letter of text using JavaScript ? Given a string and the task is to process it character by character. JavaScript String toUpperCase() Method: This method converts a string to uppercase letters. Syntax: string.toUpperCase()Parameters: This method accepts single parameter str which is required. It specifies the string to be searched. 2 min read How to Push an Object into an Array using For Loop in JavaScript ? JavaScript allows us to push an object into an array using a for-loop. This process consists of iterating over the sequence of values or indices using the for-loop and using an array manipulation method like push(), to append new elements to the array. We have given an empty array, and we need to pu 3 min read How to get the Last Nested Property Using JavaScript ? The last nested property in JavaScript is the innermost property within a hierarchy of nested objects, which can be accessed by traversing through the object. These are the following ways to get the last nested property using JavaScript: Table of Content Using recursionUsing Array.is() methodMethod 2 min read Break the Linked List in Two Halves using JavaScript A linked list is a linear data structure consisting of nodes, each containing a data element and a reference (or link) to the next node in the sequence. It provides dynamic memory allocation, and easy insertion, and deletion. Unlike arrays, linked lists do not require contiguous memory and allow eff 4 min read Like