- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the best way to break from nested loops in JavaScript?
The best way to break from nested loops is to use labels. A label can be used with break and continue 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.
Example
You can try to run the following code to implement Label with a break statement to break from nested loops −
<html> <body> <script> document.write("Entering the loop! <br /> "); outerloop: // This is the label name for (var i = 0; i < 5; i++) { document.write("Outerloop: " + i + "<br />"); innerloop: for(var j = 0; j < 5; j++) { if(j > 3 ) break ; // Quit the innermost loop if(i == 2) break innerloop; // Do the same thing if(i == 4) break outerloop; // Quit the outer loop document.write("Innerloop: " + j + " <br />"); } } document.write("Exiting the loop!<br /> "); </script> </body> </html>
- Related Articles
- What is the best way to concatenate strings in JavaScript?
- What is the best way to compare two strings in JavaScript?
- What is the best way to add an event in JavaScript?
- What is the best way to initialize a JavaScript number?
- What is the best way to do optional function parameters in JavaScript?
- Demonstrate nested loops with return statements in JavaScript?
- What is the best way to initialize a JavaScript Date to midnight?
- What is the best way of declaring multiple Variables in JavaScript?
- What is the best way to convert a number to a string in JavaScript?
- What is the best way to handle a Javascript popup using Selenium Webdriver?
- The best way to remove duplicates from an array of objects in JavaScript?
- How to use nested loops in Python?
- What is the best way to remove an item from a Python dictionary?
- What is the best way to check capacity in Java?
- What is the best way to stop Brain Drain?

Advertisements