
What is while loop
The while loop in JavaScript is a control flow statement that repeatedly executes a block of code as long as a specified condition is true. Itβs useful when you donβt know in advance how many times the loop should run.
while (condition) {
// Code to execute as long as the condition is true
}Example
let count = 0;
while (count < 5) {
console.log("Count is: " + count);
count++; // Increment the count to avoid an infinite loop
}Explanation:
What is do-while loop?
The do-while loop in JavaScript is a control flow statement that executes a block of code at least once, and then repeatedly executes the block as long as a specified condition is true. It differs from the while loop because the condition is checked after the code block is executed, ensuring the block runs at least once.
do {
// Code to execute
} while (condition);Example
let count = 0;
do {
console.log("Count is: " + count);
count++;
} while (count < 5);Explanation:
