JavaScript For Loop
JavaScript For Loop
JavaScript For Loop
❮ Previous Next ❯
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>";
Try it Yourself »
Menu Spaces Get Certified Sign Up Log in
Different
HTML CSS
Kinds of
JAVASCRIPT SQL
Loops
PYTHON JAVA PHP HOW TO W3.CSS C
Expression 1 is executed (one time) before the execution of the code block.
Expression 3 is executed (every time) after the code block has been executed.
Example
Try it Yourself »
Expression 2 defines the condition for the loop to run (i must be less than 5).
Expression 3 increases a value (i++) each time the code block in the loop has been executed.
Menu Spaces Get Certified Sign Up Log in
Expression 1
Normally you will use expression 1 to initialize the variable used in the loop (let i = 0).
This is not always the case. JavaScript doesn't care. Expression 1 is optional.
Example
Try it Yourself »
And you can omit expression 1 (like when your values are set before the loop starts):
Example
let i = 2;
let len = cars.length;
let text = "";
for (; i < len; i++) {
textMenu
+= cars[i] + "<br>"; Spaces Get Certified Sign Up Log in
}
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Try it Yourself »
Expression 2
Often expression 2 is used to evaluate the condition of the initial variable.
This is not always the case. JavaScript doesn't care. Expression 2 is also optional.
If expression 2 returns true, the loop will start over again. If it returns false, the loop will end.
If you omit expression 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.
Expression 3
Often expression 3 increments the value of the initial variable.
This is not always the case. JavaScript doesn't care. Expression 3 is optional.
Expression 3 can do anything like negative increment (i--), positive increment (i = i + 15), or
anything else.
Expression 3 can also be omitted (like when you increment your values inside the loop):
Example
let i = 0;
let len = cars.length;
let text = "";
for (; i < len; ) {
text += cars[i] + "<br>";
i++;
}
Try it Yourself
Menu
»
Spaces Get Certified Sign Up Log in
Loop Scope
Using var in a loop:
Example
var i = 5;
// Here i is 10
Try it Yourself »
Example
let i = 5;
// Here i is 5
Try it Yourself »
In the first example, using var , the variable declared in the loop redeclares the variable outside
the loop.
In the second example, using let , the variable declared in the loop does not redeclare the
Menu the loop.
variable outside Spaces Get Certified Sign Up Log in
While Loops
The while loop and the do/while are explained in the next chapters.
Exercise:
Create a loop that runs from 0 to 9.
let i;
( = ; < ; ) {
console.log(i);
}
Submit Answer »