JavaScript Tutorial
JavaScript While
while repeats while a test is true. do...while runs the body once before the first test.
Repeat while a test is true
A while loop checks a test, runs a block, then checks again. As long as the test is true, the block repeats. When the test is false, JavaScript leaves the loop and continues with the next statement.
Something inside the loop must change the values the test looks at. If nothing changes, the test never becomes false and the script does not stop.
A complete while loop
This loop prints 1 through 5. n starts at 1. Each pass records the current value, then adds 1. When n becomes 6, the test is false and the loop ends.
Example
let n = 1;
const lines = [];
while (n <= 5) {
console.log(n);
lines.push(String(n));
n = n + 1;
}
document.body.textContent = lines.join(", ");
Run this in /javascript/try. Then change 5 to another small number. Keep the increment. A missing n = n + 1 is how beginners freeze the tab.
The three parts you manage
- Set a starting value before the loop.
- Write a test that can become false.
- Update that value inside the loop.
Forget step 3 and the test stays true forever. Forget step 1 and the test uses undefined. Thefor loop packs these three into one line. while leaves them visible, which is useful when the update is not a simple plus one — waiting for a flag, shrinking a number, or draining a queue.
Drain a list
while (queue.length > 0) is a natural shape: the test is “is there still work?”shift removes the front item. Length falls. Eventually the test fails.
Example
const queue = ["draft", "review", "ship"];
const done = [];
while (queue.length > 0) {
const item = queue.shift();
console.log(item);
done.push(item);
}
document.body.textContent = "done: " + done.join(", ");
do...while runs the body first
A do...while loop runs the block once, then checks the test. If the test is true, it runs again. The body always executes at least once, even when the test starts false.
Example
let n = 8;
const lines = [];
do {
console.log(n);
lines.push(String(n));
n = n + 1;
} while (n <= 5);
document.body.textContent = "ran once: " + lines.join(", ");
n starts at 8, which already fails n <= 5. A plain while would skip the body. do...while still records 8, then checks and stops.
while versus for
| Shape | When to write it |
|---|---|
for | You know the count, or you have an index. |
while | You repeat until a condition changes. |
do...while | You must run the body once before testing. |
Infinite loops lock the preview. If the page stops responding, check that the variable in the test actually changes, and that the test can fail. Reload the editor and try a smaller bound.
What to remember
- Initialize before the loop. Update inside it. The test must be able to fail.
whilechecks first.do...whileruns once, then checks.- A shrinking list or a rising counter are the usual ways to stop.
- Next chapter:
breakleaves now;continueskips to the next pass.
Next: break and continue — leave a loop early, or skip the rest of this round.