Javascript: For Loop

The for loop allows you to repeat a code block a set number of times. For example:


for(var i = 0;i < 10;i++){
  document.write(i);
}

The for loop can be broken into three discrete parts. In the first part the variable i is set to 0. In the second part the variable is tested with a conditional operator. If the conditon is evaluated true than the code block is performed and the value is incremented using the last part of the for loop, i++.

We might also want to do something like decrement i by 2 from 100-2:


for(var i = 100;i>0;i=i-2){
  document.write(i);
}