For, While, and Do-While Loops

Loops allow you to run a block of code repeatedly.

For Loop:

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

While Loop:

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

Do-While Loop:

let i = 0;
do {
  console.log(i);
  i++;
} while (i < 5);

Use while when the number of iterations is unknown, and for when it’s predetermined.

← PrevNext →