Smal SEO Tool

JavaScript For Loop

Loops can execute a block of code a number of times.

The For Loop

The for loop has the following syntax:

javascript
for (statement 1; statement 2; statement 3) {
  // code block to be executed
}

Statement 1 is executed (one time) before the execution of the code block.

Statement 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been executed.

Example

html
let text = "";
for (let i = 0; i < 5; i++) {
  text += "The number is " + i + "<br>";
}
document.getElementById("demo").innerHTML = text;
Try it Yourself

Test Yourself with an Exercise

Which loop runs a block of code a specific number of times?