JavaScript For Loop
JavaScript For Loop
JavaScript Loops
Loops are handy, if you want to run the same code over and over again, each time with
a different value.
Instead of writing:
text += cars[0] + "<br>";
text += cars[1] + "<br>";
text += cars[2] + "<br>";
text += cars[3] + "<br>";
text += cars[4] + "<br>";
text += cars[5] + "<br>";
https://www.w3schools.com/js/js_loop_for.asp 1/12
1/16/2021 JavaScript for Loop
Try it Yourself »
Statement 1 is executed (one time) before the execution of the code block.
Statement 3 is executed (every time) after the code block has been executed.
Example
https://www.w3schools.com/js/js_loop_for.asp 2/12
1/16/2021 JavaScript for Loop
Try
it Yourself
HTML » CSS MORE EXERCISES
Statement 2 defines the condition for the loop to run (i must be less than 5).
Statement 3 increases a value (i++) each time the code block in the loop has been
executed.
OPEN
Statement 1
Normally you will use statement 1 to initialize the variable used in the loop (i = 0).
This is not always the case, JavaScript doesn't care. Statement 1 is optional.
Example
for (i = 0, len = cars.length, text = ""; i < len; i++) {
text += cars[i] + "<br>";
}
https://www.w3schools.com/js/js_loop_for.asp 3/12
1/16/2021 JavaScript for Loop
Try
it Yourself
HTML » CSS MORE EXERCISES
And you can omit statement 1 (like when your values are set before the loop starts):
Example
var i = 2;
var len = cars.length;
var text = "";
for (; i < len; i++) {
text += cars[i] + "<br>";
}
Try it Yourself »
Statement 2
Often statement 2 is used to evaluate the condition of the initial variable.
This is not always the case, JavaScript doesn't care. Statement 2 is also optional.
If statement 2 returns true, the loop will start over again, if it returns false, the loop will
end.
If you omit statement 2, you must provide a break inside the loop. Otherwise the loop
will never end. This will crash your browser. Read about breaks in a later chapter of this
tutorial.
Statement 3
Often statement 3 increments the value of the initial variable.
This is not always the case, JavaScript doesn't care, and statement 3 is optional.
https://www.w3schools.com/js/js_loop_for.asp 4/12
1/16/2021 JavaScript for Loop
Statement 3 can also be omitted (like when you increment your values inside the loop):
Example
var i = 0;
var len = cars.length;
for (; i < len; ) {
text += cars[i] + "<br>";
i++;
}
Try it Yourself »
Example
var person = {fname:"John", lname:"Doe", age:25};
Try it Yourself »
The JavaScript for/of statement loops through the values of an iterable objects.
HTML CSS MORE EXERCISES
for/of lets you loop over data structures that are iterable such as Arrays, Strings,
Maps, NodeLists, and more.
variable - For every iteration the value of the next property is assigned to the variable.
Variable can be declared with const , let , or var .
Example
var cars = ["BMW", "Volvo", "Mini"];
var x;
for (x of cars) {
document.write(x + "<br >");
}
Try it Yourself »
Example
for (x of txt) {
https://www.w3schools.com/js/js_loop_for.asp 6/12