Javascript While Loop

The while loop repeatedly executes a block of statements until a particular condition is true. It first checks the condition and executes a block of statements if the condition is true.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
while(condition){
//Block of statements
}
while(condition){ //Block of statements }
while(condition){
//Block of statements
}

JavaScript while loop example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<html>
<head>
<script>
var num=1;
while (num<=10) {
document.write(num + "<br/>");
num++;
}
</script>
</head>
<body>
</body>
</html>
<html> <head> <script> var num=1; while (num<=10) { document.write(num + "<br/>"); num++; } </script> </head> <body> </body> </html>
<html>
<head>
<script>
var num=1;
while (num<=10) {
document.write(num + "<br/>");
num++;
}
</script>
</head>
<body>
</body>
</html>