JS: Loop. for while do
By Xah Lee. Date: . Last updated: .
for-loop
for (let i = 1; i < 4; i++) {
console.log(i);
}
while-loop
let x = 1;
while (x !== 5) {
console.log(x);
x++;
}
do-while Loop
let x = 1;
do {
console.log(x);
x++;
} while (x !== 5);
continue and break
continue
- exit the current iteration in a loop.
break
- exits the loop completely.
for (let i = 1; i <= 5; i++) {
if (i === 3) {
continue;
} console.log(i);
}
for (let i = 1; i < 5; i++) {
console.log(i);
if (i === 3) break;
}