Next →

Loops in JavaScript

Loops let you run the same block of code multiple times without repeating yourself.

for Loop

Runs a block of code a specific number of times.
Example:

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

while Loop

Runs a block of code while a condition is true.
Example:

let count = 0;
while (count < 3) {
  console.log("While loop count: " + count);
  count++;
}

do...while Loop

Runs the block at least once, then repeats while the condition is true.
Example:

let num = 0;
do {
	console.log("Do-while number: " + num);
	num++;
} while (num < 2);