Do...while loops in JavaScript

by danielkwood

HTML

<html>
    <head>
        <title>Do..while loops</title>
        <script src="script.js"></script>
    </head>
        
    <body>

    </body>
</html>

JavaScript

// Set the counter to 1
var counter = 1;

// This is an example of a do-while loop
// Keep counting up by 1 until the counter reaches 10

do{
    console.log(counter);
    counter++;
}while(counter <= 10);

// Change the counter on Line 1 to 20 and run the program again. What happens?
// The number '20' will display in the console because it runs the loop once before checking the condition.
// We can see the block of code in the loop runs at least
// once even if the test condition evaluates to false.

console.log("Goodbye!");