how to use while loop javascript code example
Example 1: javascript while
var i=0;
while (i < 10) {
console.log(i);
i++;
}
//Alternatively, You could break out of a loop like so:
var i=0;
while(true){
i++;
if(i===3){
break;
}
}
Example 2: javascript count down with wile loop
//declare and instantiate your variable
var timer = 60;
//set up your loop that will run while your variable is greater than zero
while (timer > 0) {
//log timer to the console for debugging purposes
console.log(timer);
//reduce the count of timer by one over each iteration
timer--;
}
//after timer has reached zero, the code will break out of the while loop and run the alert with your confirmation message
alert("Done");
Example 3: while javascript
do {
text += "The number is " + i;
i++;
}
while (i < 10);