
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Difference Between Break and Continue Statements in JavaScript
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>