Javascript For Loop Control

The Javascript for loop control statement will execute a block of code, when you already know how many times script should perform or you want loop through a block of code to specific number of time.

Syntax

<script type="text/javascript">
for (variable=startindex; variable<=endindex; variable=variable+increment) 
{
// Code block to be executed.
}
</script>

Javascript For Loop Control Example

<!DOCTYPE html>
<html>
<head>  <!-- www.techstrikers.com -->
<title>My first Javascript code</title>   
</head>  
<body bgcolor="#bnde45">
<fieldset>
<legend>Javascript For Loop</legend>
<script type="text/javascript">
for (j=0; j<=10; j++)
{
document.write("<b>The number is " + j + "</b>")
document.write("</br>")
}
</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 5, 
the loop will continue to run.
<p<>b>i<b> will increase by 1 each time the loop runs.
</fieldset>
</body>
</html>