while, do...while, break and continue
while checks its condition before each pass and do...while checks after, so it always runs at least once. break leaves the loop immediately and continue jumps to the next pass.
Make sure something inside the loop changes the condition, or it will run forever.
Example
let n = 0;
while (n < 5) {
n++;
if (n === 2) continue;
if (n === 4) break;
console.log("n is " + n);
}
let tries = 0;
do {
tries++;
} while (tries < 3);
console.log("tries: " + tries);Exercise
Leave the loop early.
while (true) { ; }
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.