Advertisement

Google Ad Slot: content-top

JS while and do while


while Loop:

The while loop executes as long as the specified condition evaluates to true. The condition is evaluated before the code block executes.


Syntax:

while (condition) { 
  // Code to execute 
}
Example
let count = 0;

while (count < 5) {
console.log(count);
count++;
}
Try it yourself

do...while Loop:

The do...while loop executes the code block at least once, regardless of the condition. The condition is evaluated after the code block executes.


Syntax:

do {
    // Code to execute
} while (condition);
Example
let count = 0;

do {
console.log(count);
count++;
} while (count < 5);
Try it yourself