What is the syntax for JavaScript for loop?The syntax for the JavaScript for loop is the following:
for(initialization;condition for looping;update of starting value) {
//The code that should be repeated
}
|
What is an example of a JavaScript for loop?In the following example the JavaScript for loop will execute the code 5 times. The starting value for the variable i in the JavaScript for loop is 0. With each iteration of the JavaScript for loop, the variable i is incremented by 1. The JavaScript for loop will stop as soon as the variable i reaches the value 5.
Example:
var i = 0;
for (i = 0; i < 5; i++) {
document.write("JavaScript for Loop: " + i + "<br />");
}
The resulting output for this JavaScript for loop is: JavaScript for Loop: 0
JavaScript for Loop: 1
JavaScript for Loop: 2
JavaScript for Loop: 3
JavaScript for Loop: 4
|