×

typescript · level 1 · unit 5 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A does the same job again and again.

A loop repeats a block of code. Picture laps of a running track: same route, again and again, until you have done enough. A for loop is a loop that counts its own laps.

Its round brackets hold three parts, split by semicolons. let lap = 1 is where to start. lap <= 3 is the keep-going test. lap++ is the step: adds one to the box.

Each lap, the block inside the curly brackets runs, and lap holds the number of that lap. When the test turns false, the loop ends and the program carries on below.

for (let lap = 1; lap <= 3; lap++) {
  console.log("Lap", lap);
}
console.log("Finished!");

No type label is needed in let lap = 1, because TypeScript can see that 1 is a number.