Break and Continue statement in javascript.
- Introduction of Break statement
- Syntax of break statement
- Example of break statement
- Introduction to Continue statement
- Syntax of continue statement
- Example of Continue statement
Break statement is used to exit out of loop. When you want to exit out of loop early, break keyword is used. As break keyword is encountered inside loop, control passes to to the statement after loop. The break statement breaks loop and continue the statement or code after the loop.
Syntax of break statement
break;
Example of break statement
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Break Statement</h2>
<script type="text/javascript">
var i;
for (i = 0; i < 10; i++)
{
if (i === 4)
{
break;
}
document.write("The number is " + i + "<br>");
}
</script>
</body>
</html>
Output-
JavaScript Break Statement
The number is 0
The number is 1
The number is 2
The number is 3
Introduction to Continue statement
When you want to break one iteration in the loop, continue statement is used. This continue statement breaks loop iteration and continue next iteration in the loop.
Syntax of continue statement
continue;
Example of Continue statement
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Continue Statement</h2>
<script type="text/javascript">
var i;
for (i = 0; i < 5; i++)
{
if (i === 2)
{
continue;
}
document.write("The number is " + i + "<br>");
}
</script>
</body>
</html>
Output-
JavaScript Continue Statement
The number is 0
The number is 1
The number is 3
The number is 4
Important Note-
Here condition value is i==2 matches, break one iteration in the loop and continue next iteration in the loop so do not show 2 in output but continue from display numbers from 3 to last number 5.
Please Comment and Share. ConversionConversion EmoticonEmoticon