Loops in JavaScript allow you to execute a block of code multiple times.
There are mainly three types of loops in JavaScript: for
, while
, and do-while
.
//for loop
for (let i = 0; i < 5; i++) {
console.log(i);
}
//while loop
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
// do while loop
let i = 0;
do {
console.log(i);
i++;
} while (i < 5);
//Loop control statements
for (let i = 0; i < 5; i++) {
if (i === 2) {
continue; // Skip the current iteration
}
if (i === 4) {
break; // Exit the loop
}
console.log(i);
}
© www.thecoderjob.com. All Rights Reserved. Designed by HTML Codex