EN
JavaScript - for statement
0 points
In this article, we would like to show you for statement to create loops in JavaScript.
xxxxxxxxxx
1
for ([initial-expression]; [condition-expression]; [final-expression]) {
2
// statement
3
}
Note:
All three expressions in the head of the
for
loop are optional (initial-expression
,condition-expression
andfinal-expression
). For more details see the examples below.
In this example, we declare i
variable and initialize it with 0
. the for
statement checks if i
is less than 3
, performs the statement inside the braces and increments i
by 1 after each pass through the loop.
xxxxxxxxxx
1
for (let i = 0; i < 3; ++i) {
2
console.log(i);
3
}
In this example, we skip the initial-expression
in the head of the for loop.
xxxxxxxxxx
1
let i = 0
2
3
for (; i < 3; ++i) {
4
console.log(i);
5
}
In this example, we omit the condition-expression
.
xxxxxxxxxx
1
for (let i = 0; ; i++) {
2
console.log(i);
3
if (i > 3) break;
4
}
In this example, we omit all three blocks.
xxxxxxxxxx
1
let i = 0;
2
3
for (;;) {
4
if (i > 3) break;
5
console.log(i);
6
i++;
7
}