
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Describe JavaScript Break, Continue and Label Statements
break statement
The break statement is used to exit a loop early, breaking out of the enclosing curly braces. The break statement exits out of a loop.
Let’s see an example of break statement in JavaScript. The following example illustrates the use of a break statement with a while loop. Notice how the loop breaks out early once x reaches 5 and reaches to document.write (..) statement just below to the closing curly brace
Example
<html> <body> <script> var x = 1; document.write("Entering the loop<br /> "); while (x < 20) { if (x == 5) { break; // breaks out of loop completely } x = x + 1; document.write( x + "<br />"); } document.write("Exiting the loop!<br /> "); </script> </body> </html>
continue statement
The continue statement tells the interpreter to immediately start the next iteration of the loop and skip the remaining code block. When a continue statement is encountered, the program flow moves to the loop check expression immediately and if the condition remains true, then it starts the next iteration, otherwise, the control comes out of the loop.
The continue statement breaks over one iteration in the loop. This example illustrates the use of a continue statement with a while loop. Notice how to continue statement is used to skip printing when the index held in variable x reaches 8
Example
<html> <body> <script> var x = 1; document.write("Entering the loop<br /> "); while (x < 10) { x = x + 1; if (x == 8) { continue; // skip rest of the loop body } document.write( x + "<br />"); } document.write("Exiting the loop!<br /> "); </script> </body> </html>
Label statement
JavaScript label statements are used to prefix a label to an identifier. A label can be used with break and continue statement 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. We will see two different examples to understand how to use labels with break and continue.
You can try to run the following code to use labels to control the flow, with break statement
Example
<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 Questions & Answers
- break, continue and label in Java loop
- Difference between continue and break statements in Java
- What is the difference between break and continue statements in JavaScript?
- How to use break and continue statements in Java?
- Loops and Control Statements (continue, break and pass) in Python
- What is the difference between break and continue statements in C#?
- How to control for loop using break and continue statements in C#?
- Difference Between break and continue
- What are label statements in JavaScript?
- Why are "continue" statements bad in JavaScript?
- What difference between break and continue in C/C++?
- What is the difference between break with a label and without a label in JavaScript?
- How can I use a label with continue statement in JavaScript?
- 'break' and 'continue' in forEach in Kotlin
- What are the synonym statements of MySQL DESCRIBE?