JavaScript While Loop

While writing a program, you may encounter a situation where you need to perform an action over and over again. In such situations, you would need to write loop statements to reduce the number of lines.
JavaScript supports all the necessary loops to ease down the pressure of programming.

The most basic loop in JavaScript is the while loop which would be discussed in this chapter. The purpose of a while loop is to execute a statement or code block repeatedly as long as an expression is true. Once the expression becomes false, the loop terminates.

Syntax:

while (expression) {
// Statements to be executed if condition is true
}

Try the following example to learn how a while loop works in JavaScript.

Example:

var count = 0;
while (count < 10) {
document.write ("Current Count : " + count );
count++;
}

Run Example