Repeat Instructions With the for Loop in JavaScript

6 min read·Jan 1, 2026

The for loop, inspired from the C language, is used to execute a set of statements for as long as the specified condition remains true. This loop is mostly used when the number of iterations is known or predictable, like for example, a counter or a range.

for (initialization ; condition ; afterthought) {
  statements
}

Where:

  • initialization is an optional expression executed once at the beginning of the loop and is typically used to set the initial values for loop control variables.
  • condition is an optional expression evaluated before each iteration of the loop and keeps the loop running until it evaluates to false.
  • afterthought is an optional expression executed at the end of each iteration and is typically used to update loop control variables.

💡 Any variable declared within the initialization will only be accessible within the loop's block statement ({}).

Example

Let's consider this script, that outputs a countdown:

for (let countdown = 3 ; countdown > 0 ; countdown--) {
  console.log(countdown);
}
console.log("Liftoff!");

When executed, it will:

  1. Define a for loop.

    1. Initialization -> Define a countdown variable and initialize it with the integer 3. This variable will be used to keep track of the launch countdown.
    2. Condition -> Check if countdown is greater than 0 before executing the loop.
    3. Block statement -> Output the current value of countdown.
    4. Afterthought -> Decrement the value of countdown by 1.
  2. Output the string "Liftoff!".

Which will produce this output:

3
2
1
Liftoff!

Example

Let's consider this script, that outputs the greatest common divisor of two integers:

const a = 56;
const b = 24;
const smallestValue = a <= b ? a : b;
let greatestDivisor = 1;

for (let divisor = 1 ; divisor <= smallestValue ; divisor++) {
  if (a % divisor === 0 && b % divisor === 0) {
    greatestDivisor = divisor;
  }
}

console.log(greatestDivisor);

When executed, it will:

  1. Define a a variable and initialize it with the integer 56.

  2. Define a b variable and initialize it with the integer 24.

  3. Define a smallestValue variable and initialize it with the smallest value between a and b.

  4. Define a greatestDivisor variable and initialize it with the integer 1, which is the smallest common divisor of a and b.

  5. Define a for loop, where:

    • let divisor = 0 is used to initialize a variable named divisor that will be used by the loop at each iteration.
    • divisor <= smallestValue is used to evaluate whether the current value of the divisor variable equals the value of the smallest number and stop the loop if true.
    • divisor++ is used to increment the value of the divisor variable by 1 at each iteration of the loop.
  6. Evaluate whether the a and b variables are both divisible by the divisor variable and store its current value into the greatestDivisor variable if true.

  7. Output the value of the greatestDivisor variable after the for loop terminates.

Which will produce this output:

8

Enjoying the courses?

I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:

Support BackendBrewery
Repeat Instructions With the `for` Loop in JavaScript | Backend Brewery