User Rating: 2 / 5

Star ActiveStar ActiveStar InactiveStar InactiveStar Inactive
 

The for loop is the most compact form of looping and includes the following three important parts:

The loop initialization where we initialize our counter to a starting value. The initialization statement is executed before the loop begins.

The test statement which will test if the given condition is true or not. If condition is true then code given inside the loop will be executed otherwise loop will come out.

The iteration statement where you can increase or decrease your counter.

You can put all the three parts in a single line separated by a semicolon.

Syntax:

for (initialization; test condition; iteration statement){
 Statement(s) to be executed if test condition is true
}

Example:

<html>
<body>
<script type="text/javascript">
var count;
for(count = 0; count < 10; count++){
  document.write("Number : " + count );
  document.write("<br />"); }
</script>
</body>
</html>

This is the result:

 

 

Next

Chapter 15 - JavaScript While Loop