2. while Statement
The while statement is one of the simplest types of loop statements, which repeats a statement or sequence of statements as long as they are evaluated by a given conditional expression to a truthy value.
Syntax:
In the while statement, the conditional expression is enclosed within a parenthesis following the keyword while. In general, a conditional expression contains a variable whose value in the statement code is modified by one or more statements. Each time the statements finish executing, the expression is reevaluated.
If a true value is determined by the expression, the statements are executed again; if a false value is evaluated by the expression, then the loop ends. Each repetition of a looping statement is known as iteration.
You must have a code that monitors the progress of the loop and adjusts the value provided by the conditional statement to ensure that the iteration ends after the desired tasks have been completed. You can monitor the progress with a counter for a while statement, each iteration of a loop statement, a counter which is a variable, is incremented (increased) or decremented (decreased) until the desired value is reached that would make the while condition to be false and end the loop.
The following code shows a simple script containing a while statement. The script declares the count variable and assigns it an initial value of 1. The count variable is then used in the condition (count <= 5) for the while statement. The while statement loops as long as the count variable is less than or equal to 5. The document.write () statement prints the value of the count variable within the body of the while statement, then the count variable increments by a value of 1. The while statement loops until a value of 6 is reached by the count variable.
while (count <= 5) {
count++;
document.write("You have printed 5 numbers.");
The code prints the numbers 1 through 5, with one iteration of the loop representing each number. When the count hits 6, the "You have printed 5 numbers" message prints, thus showing that the loop is over.
Output:

In a while loop, you can also control the repetitions by decreasing (decrementing the value of) the count variable.
while (count > 0) {
count--;
document.write("We have taken off. ");
In this case, the count variable's initial value is 10 and the decrement operator (--) is used to reduce the count by one. If the count variable is greater than 0, the value of the count variable is shown by the expression in the while loop. The while loop ends when the count value is equal to 0, and the statement immediately after the while loop is printed on the browser.

To control the repetitions of a while loop, there are
several ways to change the value of a count variable and to use it. The example
below uses the assignment operator *= to multiply by 2 the value of the count
variable. The while statement ends when the count variable reaches a value of
128, which is greater than 100, and the value 128 is not printed.
while (count <= 100) {
count *= 2;
As mentioned before, you must have a code inside the command block that changes the value used by the conditional expression, to ensure that a while statement ends eventually. Your program will be stuck in an infinite loop if you do not include code that changes this value. A loop statement never ends in an infinite loop because its conditional expression is never falsified.
window.alert("The number is " + count + ".");
Although a conditional expression that checks the value of a count variable is included in the while statement in the previous example, there is no code within the command block that changes the value of the count variable. Through each iteration of the loop, the count variable will continue to have the same value. That means, no matter how many times the user clicks the OK button, a warning dialog box containing the text string 'Number is 1.' will appear over and over again.
