Repeat Instructions With the for Loop in JavaScript

JavaScript·2 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);}

Unlock the program 🚀

Pay once, own it forever.

€79

30-day money-back guarantee

  • 13 modules
  • 113 lessons with full-code examples
  • 29 projects with commented solutions
  • All future lesson and project updates
  • Lifetime access

By submitting this form, you agree to the Terms & Conditions and Privacy Policy.