javascript-bucle-for

What is and how to use the FOR loop in JavaScript

  • 6 min

A for loop is a control structure that repeats a block of code a specific number of times. The for loop is a very common control structure in JavaScript.

If you want to learn more, check out the Introduction to Programming Course

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

for (initialization; condition; update)
{
    // Instructions to execute in each iteration
}
Copied!
  • initialization is executed at the start of the loop, typically used to initialize a variable.
  • condition is evaluated at the start of each iteration; if true, the code inside the loop is executed; if false, the loop is exited.
  • update is executed at the end of each iteration, typically used to update the value of a variable.

Basic Example

Let’s see a simple example where a for loop is used to print numbers from 1 to 10:

for (let i = 1; i <= 10; i++) {
    console.log(i);
}
Copied!

In this example:

  • The initialization let i = 1 sets the initial value of i to 1.
  • The condition i <= 10 ensures the loop runs while i is less than or equal to 10.
  • The update i++ increments i by 1 after each iteration.
  • Inside the loop, we use console.log() to print the current value of i to the console.

The result is that numbers from 1 to 10 will be displayed on the screen.

Modifying Loop Flow

Skipping Iterations with continue

The continue statement is used to skip the current iteration and move directly to the next iteration of the loop.

for (let i = 0; i < 10; i++) {
    if (i % 2 === 0) {
        continue; // Skip even numbers
    }
    console.log(i);
}
Copied!

In the example, the conditional would skip all even numbers, so only odd numbers would be printed to the screen.

Interrupting the Loop with break

Using the break statement allows exiting the loop before the condition is met. This is useful in situations where a specific value is found and further iteration is unnecessary.

for (let i = 0; i < 10; i++) {
    if (i === 5) {
        break;  // interrupt upon reaching 5
    }
    console.log(i);
}
Copied!

In this case, the loop would run from 0, but the conditional would interrupt it upon reaching 5. So it would display 0, 1, 2, 3, and 4 on the screen.

Special Cases

It is possible to create more “unusual” versions of the for loop. In general, it’s not a good idea to do this. But I’m telling you about it, if only so that if you ever see it, you can give someone a hard time. 😉

Using External Variables

It is possible to use a variable declared outside the for loop as the control variable. However, this can lead to confusion and errors if not managed properly:

let i = 0;
for (i = 0; i < 5; i++) {
    console.log(i);
}
console.log(`Value of i after the loop: ${i}`);
Copied!

In this case, i retains its value after the loop has finished, which will be 5.

Multiple Statements

In the initialization and update sections, you can include multiple statements separated by commas. This is useful when you need several control variables:

for (let i = 0, j = 10; i < j; i++, j--) {
    console.log(`i: ${i}, j: ${j}`);
}
Copied!

Practical Examples

These examples aim to show how to use the for loop. It does not mean it’s the best way to solve the problem they address. Usually, there are better alternatives.