Loops

Loops repeat a block of code multiple times, so you don't have to write the same lines over and over.

The for loop

A for loop has three parts: a starting point, a condition to keep going, and what happens after each pass. Try changing i <= 5 to a different number:

Try it yourself
Console output

The while loop

A while loop keeps running as long as its condition stays true. You're responsible for changing something inside the loop so it eventually stops:

Try it yourself
Console output
Note: forgetting to update the loop variable inside a while loop creates an infinite loop, which will freeze the page. Always make sure the condition can eventually become false.