JavaScript / Loops

Repeating with for

Repeat commands a set number of times with a for loop, and use its counter inside.

To print a word ten times you could copy the line ten times, but that is tiring and harder to change. A for loop writes the command once and repeats it as many times as you say:

JavaScript
for (let i = 0; i < 3; i++) {
  console.log("Red lorry, yellow lorry");
}
Console
Red lorry, yellow lorry
Red lorry, yellow lorry
Red lorry, yellow lorry

The brackets hold three parts separated by semicolons. They control how many times the loop runs, using a variable called the counter:

PartIn the exampleMeaning
Startlet i = 0The counter starts at 0
Conditioni < 3The loop keeps going while this is true
Updatei++The counter goes up by 1 after each round

i++ is short for i = i + 1. The counter is an ordinary variable, so you can print it to see its value each round:

JavaScript
for (let i = 0; i < 5; i++) {
  console.log(i);
}
Console
0
1
2
3
4

To count from 1, start at 1 and use <=, and join the counter to text with +:

JavaScript
for (let lap = 1; lap <= 4; lap++) {
  console.log("Lap " + lap);
}
Console
Lap 1
Lap 2
Lap 3
Lap 4
Starting at 0 with i < 10 repeats 10 times. Starting at 1 with i < 3 repeats only twice (1 and 2). Always check the start and the condition together.

switch inside for

In the switch lesson we put one student into a group using the remainder. Now one loop repeats the same check for every student in the class:

JavaScript
for (let num = 1; num <= 8; num++) {
  switch (num % 4) {
    case 1:
      console.log("Student " + num + " is in Group A");
      break;
    case 2:
      console.log("Student " + num + " is in Group B");
      break;
    case 3:
      console.log("Student " + num + " is in Group C");
      break;
    case 0:
      console.log("Student " + num + " is in Group D");
      break;
  }
}
Console
Student 1 is in Group A
Student 2 is in Group B
Student 3 is in Group C
Student 4 is in Group D
Student 5 is in Group A
Student 6 is in Group B
Student 7 is in Group C
Student 8 is in Group D
A counter declared in the brackets of for does not exist outside the loop; using it afterwards causes an error:
JavaScript
for (let i = 0; i < 2; i++) {
  console.log(i);
}
console.log(i);
Console
0
1
ReferenceError: i is not defined

Practice

1Fill in the blankCurriculum practice

Fill in the brackets so silk is printed ten times, with a counter named count starting at 0.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
for (___) {
  console.log("silk");
}
Console
Press Run to see the result.
2Write the programCurriculum practice

Print Good morning seven times with a for loop, counter i starting at 0, using i++.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
3Fill in the blankCurriculum practice

Fill in the two blanks so the program prints Hello three times (counter i starts at 0).

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
___ (___) {
  console.log("Hello");
}
Console
Press Run to see the result.
4Fill in the blankCurriculum practice

Students 1 to 10 are shared between three groups by the remainder of i divided by 3. Fill in the blanks.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
___ (let i = 1; i <= 10; i++) {
  ___ (___) {
    case 0:
      console.log("Student " + i + " is in Group C");
      break;
    case 1:
      console.log("Student " + i + " is in Group A");
      break;
    case 2:
      console.log("Student " + i + " is in Group B");
      break;
  }
}
Console
Press Run to see the result.
5Write the programExtra practice

A test has 5 questions. Print Question 1 of 5, then Question 2 of 5, and so on up to Question 5 of 5.

HintStart the counter at 1 and use <=.
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
6Write the programExtra practice

Print the multiples of 5 from 5 to 50, one per line. Make the update i = i + 5 instead of i++.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.

Key points

  • for (let i = 0; i < n; i++) { … } repeats the commands n times.
  • i++ means i = i + 1, and the counter is a variable you can print and use.
  • A counter declared in for cannot be used outside the loop.