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:
initializationis an optional expression executed once at the beginning of the loop and is typically used to set the initial values for loop control variables.conditionis an optional expression evaluated before each iteration of the loop and keeps the loop running until it evaluates tofalse.afterthoughtis an optional expression executed at the end of each iteration and is typically used to update loop control variables.
💡 Any variable declared within the
initializationwill 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:
-
Define a
forloop.- Initialization -> Define a
countdownvariable and initialize it with the integer3. This variable will be used to keep track of the launch countdown. - Condition -> Check if
countdownis greater than 0 before executing the loop. - Block statement -> Output the current value of
countdown. - Afterthought -> Decrement the value of
countdownby 1.
- Initialization -> Define a
-
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:
-
Define a
avariable and initialize it with the integer56. -
Define a
bvariable and initialize it with the integer24. -
Define a
smallestValuevariable and initialize it with the smallest value betweenaandb. -
Define a
greatestDivisorvariable and initialize it with the integer1, which is the smallest common divisor ofaandb. -
Define a
forloop, where:let divisor = 0is used to initialize a variable nameddivisorthat will be used by the loop at each iteration.divisor <= smallestValueis used to evaluate whether the current value of thedivisorvariable equals the value of the smallest number and stop the loop iftrue.divisor++is used to increment the value of thedivisorvariable by1at each iteration of the loop.
-
Evaluate whether the
aandbvariables are both divisible by thedivisorvariable and store its current value into thegreatestDivisorvariable iftrue. -
Output the value of the
greatestDivisorvariable after theforloop 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