while loop countdown javascript – javascript count down with wile loop Example

while loop countdown javascript – JavaScript while loop examples as well as The while loop loops through a block of code as long as a specified condition is true.

while loop countdown javascript

first of all declare and instantiate your variable and then set up your loop that will run while your variable is greater than zero, log timer to the console for debugging purposes and reduce the count of timer by one over each iteration.

JavaScript while Loop

while (expression) {
    // statement
}

javascript count down with wile loop

javascript count down with wile loop

var userCountDown = 60;


while (userCountDown > 0) {

console.log(userCountDown);

userCountDown--;
}

//after userCountDown has reached zero, the code will break out of the while loop and run the alert with your confirmation message
alert("Done");

JavaScript while loop examples

let userCountDown = 1;
while (userCountDown < 10) {
    console.log(userCountDown);
    userCountDown +=2;
}

create an array of five random number between 1 and 10

let rands = [];
let userCountDown = 0;
const base = 5;

while(userCountDown < base) {
    rands.push(Math.round(Math.random() * 10));
    userCountDown++;
    console.log('The Active base of the array is ' + userCountDown);
}

console.log(rands);

Results

The Active base of the array is 1
The Active base of the array is 2
The Active base of the array is 3
The Active base of the array is 4
The Active base of the array is 5

[1, 9, 1, 9, 6]

How to make while loops countdown seconds in javascript?

Here's an example of how to create a countdown in seconds using a while loop in JavaScript:

var count = 10;

while (count > 0) {
    console.log(count + " seconds remaining");
    count--;
    // wait for 1 second
    setTimeout(function () {}, 1000);
}

console.log("Countdown complete");

In this example, the count variable is initialized to 10 and the while loop continues as long as count is greater than 0. Inside the loop, the current value of count is displayed, count is decremented by 1, and the setTimeout function is used to pause the loop for 1 second (1000 milliseconds).

After the while loop finishes, a message indicating that the countdown is complete is displayed. Note that this code will only work correctly if run in an environment that supports setTimeout (such as a browser JavaScript console).

Don't Miss : JQuery Tips And Tricks With Example

I hope you get an idea about while loop countdown javascript.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment