Web Analytics

JavaScript Loops

Beginner ~15 min read

The For Loop

Loops can execute a block of code a number of times. The for loop is the most common loop.

for (expression 1; expression 2; expression 3) {
  // code block to be executed
}
HTML
CSS
JS

The While Loop

The while loop loops through a block of code as long as a specified condition is true.

HTML
CSS
JS

Do...While Loop

The do...while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

HTML
CSS
JS

Break and Continue

The break statement "jumps out" of a loop.
The continue statement "jumps over" one iteration in the loop.

HTML
CSS
JS

Summary

  • for - loops through a block of code a number of times
  • while - loops through a block of code while a specified condition is true
  • do...while - also loops through a block of code while a specified condition is true
  • break - exits the loop
  • continue - skips the current iteration and continues with the next

Quick Quiz

Which statement skips the current iteration of a loop?

A
break
B
stop
C
continue
D
skip