JavaScript for Loop
Loops can execute a block of code a specified number of times.
JavaScript loops
Loops are convenient when you want to run the same code over and over again, with a different value each time.
We can output the values of an array like this:
General approach:
document
.
write
(
cars
[
0
]
"
"
)
;
document
.
write
(
cars
[
1
]
"
"
)
;
document
.
write
(
cars
[
2
]
"
"
)
;
document
.
write
(
cars
[
3
]
"
"
)
;
document
.
write
(
cars
[
4
]
"
"
)
;
document
.
write
(
cars
[
5
]
"
"
)
;
Using a for loop
for
(
var
i
=
0
;
i
<
cars
.
length
;
i
++
)
{
document
.
write
(
cars
[
i
]
"
"
)
;
}
Try it »
Different Types of Loops
JavaScript supports different types of loops:
- for - Loop through a block of code a number of times
- for/in - Loop through the properties of an object
- while - Loop through a block of code while a specified condition is true
- do/while - Also loops a specified code block while the specified condition is true
For Loop
The for loop is a tool you will often use when you want to create a loop.
The syntax of a for loop is:
for (
Statement 1
;
Statement 2
;
Statement 3
)
{
the block of code to be executed
}
Statement 1 executed before the (code block) starts
Statement 2 defines the condition for running the loop (code block)
Statement 3 executed after the loop (code block) has been executed
Example
for
(
var
i
=
0
;
i
<
5
;
i
++
)
{
x
=
x
"
The number is
"
i
"
"
;
}
Try it »
From the example above, you can see that:
Statement 1 sets a variable before the loop starts (var i=0).
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 has been executed.
Statement 1
Usually we use Statement 1 to initialize the variable used in the loop (var i=0).
Statement 1 is optional; you can omit it.
You can initialize any (or multiple) values in Statement 1:
Example:
for
(
var
i
=
0
,
len
=
cars
.
length
;
i
<
len
;
i
++
)
{
document
.
write
(
cars
[
i
]
"
"
)
;
}
Try it »
You can also omit Statement 1 (for example, when the value is already set before the loop starts):
Example:
var
i
=
2
,
len
=
cars
.
length
;
for
(
;
i
<
len
;
i
++
)
{
document
.
write
(
cars
[
i
]
"
"
)
;
}
Try it »
Statement 2
Statement 2 is usually used to evaluate the condition of the initial variable.
Statement 2 is also optional.
If Statement 2 returns true, the loop starts again; if it returns false, the loop ends.
![]() |
If you omit Statement 2, you must provide a break inside the loop break. Otherwise the loop cannot stop, which may crash the browser. Read about break in a later chapter of this tutorial. |
|---|
Statement 3
Statement 3 usually increases the value of the initial variable.
Statement 3 is also optional.
Statement 3 has several uses. The increment can be negative (i--) or larger (i=i+15).
Statement 3 can also be omitted (for example, when the loop contains the relevant code):
Example:
var
i
=
0
,
len
=
cars
.
length
;
for
(
;
i
<
len
;
)
{
document
.
write
(
cars
[
i
]
"
"
)
;
i
++;
}
Try it »
The for/in Loop
The JavaScript for/in statement loops through the properties of an object:
Example
var
person
=
{
fname
:
"
Bill
"
,
lname
:
"
Gates
"
,
age
:
56
}
;
for
(
x
in
person
)
//
x is the property name
{
txt
=
txt
person
[
x
]
;
}
Try it »
You will learn more about for/in loops in the chapter on JavaScript objects.
While Loop
In the next chapter, we will explain while loops and do/while loops.
