The for and for-in loops

In this lesson, you will have an overview of all flow-control statements provided by the JavaScript language.

The for Loop

Just as in many programming languages, the for loop provides a pretest with optional variable initialization and optional post-loop code to be executed:

Illustration #

Here is an illustration to explain the above concept

Syntax #

The syntax of the for loop in JavaScript is as follows:

Press + to interact
for (initialization ; condition ; post_loop_expression) statement

Example #

This example shows the usage of a for loop:

Press + to interact
for (var i = 0; i < 10; i++) {
console.log(i);
}

The initialization, condition, and post_loop_expression are all optional, so you can create an infinite loop by omitting all of them:

Press + to interact
for (;;;) {
console.log("Nothing gonna stop me");
}
...