Skip to content
dreamcode
dreamcode
Map
While loops
Lesson 13 of 48
+15 XP on finish
JS LOOPSChapter 3 · JS Collections & Loops

While Loops

A while loop repeats its block for as long as the condition is true, checking before every pass. Use it when you do not know in advance how many times to loop. Something inside the loop must change the condition, or it never ends. A do...while loop runs its block once before the first check.

Worked example

How it reads

  • The condition is checked before each pass, so the loop may run zero times
  • fuel -= 3 moves the loop toward its end
  • do...while always runs at least once
Cloud tip: If the runner stops your code for running too long, look for a while loop whose condition never becomes false.
index.js
JAVASCRIPT
real JavaScript, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Use a while loop to keep doubling n while it is 100 or less, then print it: 128.

Press Run to check your work.