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.


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

Javascript For Loop Control Example

<!DOCTYPE html>
<head>  <!-- -->
<title>My first Javascript code</title>   
<body bgcolor="#bnde45">
<legend>Javascript For Loop</legend>
<script type="text/javascript">
for (j=0; j<=10; j++)
document.write("<b>The number is " + j + "</b>")
<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.