Javascript Loop Control

In Javascript, when required you can stop or come out from the loop by placing break statement in the Javascript Loop Control.

The Break Statement

The break statement is used to come out or stop the loop.

Syntax

<script type="text/javascript">
for(int i=0;i<=10;i++){
   if(true)
	break;
} 
</script>

Note: The <= could be anything that would fit the purpose ex. >, == or whatever.

Javascript Do While Loop Example

<!DOCTYPE html>
<html>
<head>  <!-- www.techstrikers.com -->
<title>My first Javascript code</title>   
</head>  
<body bgcolor="#bnde45">
<fieldset>
<legend>Javascript Do While Loop</legend>
<script type="text/javascript">
for (j=0; j<=10; j++)
{
	if(j > 5){
		document.write("<b>The number is " + j + "</b>")
		document.write("</br>");
		break;
	}
}
</script>
<p>Explanation:
<p>The for loop sets <b>i<b> equal to 0.
<p>As long as <b>i</b> is less than or equal to 10, 
the loop will continue to run.
<p><b>i</b> will increase by 1 each time the loop runs.
</fieldset>
</body>
</html>