Tsallaka zuwa abun ciki
Justice Tech School

Loops

A for loop has three parts: a start, a condition and a step. A while loop repeats while its condition stays true.

Be careful: a loop that never ends will freeze the page. If that happens in the editor, close and reopen the tab.

Example
for (let i = 1; i <= 5; i++) {
  console.log("Number " + i);
}

let n = 3;
while (n > 0) {
  console.log("Countdown " + n);
  n--;
}

Exercise

Count from 0 to 4.

for (let i = 0; i  5; i++) {
  console.log(i);
}

Quiz Test

What does i++ do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.