The JavaScript While Loop is used to repeat a block of statements for a given number of times until the given condition is False. The JavaScript While loop starts with the condition, if the condition is True, statements inside it will execute. If it is false, it won’t run at least once. It means, JavaScript while loop may execute zero or more time.
JavaScript While Loop Syntax
The syntax of the JavaScript While loop is as follows:
While( Condition ) { statement 1; statement 2; …………. } It is the statement Outside the While Loop
First, the compiler checks the condition inside the JavaScript While loop. If the condition is True, the statement or group of statements under the while loop block executed. If it is False, the compiler will come out of it and execute other statements outside the while loop.
For a single statement in the While loop, curly braces not required. However, if we omit them for multiple statements, it will execute the first statement only. Always use braces.
JavaScript While Loop Flow Chart
The flow chart of JavaScript While loop is as shown below:

The JavaScript While loop will check for the condition at the beginning of the loop.
- If the condition is True, it executes the code inside the while loop.
- Next, we have to use Increment & Decrement Operator inside it to Increment the value
- Again it will check the expression after the value incremented. As long as the condition is True, the code inside the while loop executed.
- If it is False, it will exit from it.
Let us see the While loop example for better understanding
JavaScript While Loop Example
In this program, we declared a number variable and assigned value 6. By using it, the compiler will add those values up to 10.
<!DOCTYPE html> <html> <head> <title> Example </title> </head> <body> <h1> Example </h1> <script> var number = 6, total=0; while (number <= 10) { total = total + number; number++; } document.write("Total = ", total); </script> </body> </html>
ANALYSIS
- In this JavaScript while Loop example, First, the value inside the number variable (6) is tested against the condition.
- If the condition results true, the number added to the total. Otherwise, it will exit from the JavaScript loop
- In the next line, we used ++ operator to increment the number value. After incrementing, the process repeated until the condition results False.
Here, Our number = 6. It means, total = 6+7+8+9+10 = 40
Example
Total = 40
Infinite JavaScript While Loop
If you forgot to increment or decrement the value inside the while loop, then it will execute infinite times (also called as infinite loop). For example:
<!DOCTYPE html> <html> <head> <title> JavaScriptWhile Loop </title> </head> <body> <h1> JavaScriptWhile Loop </h1> <script> var x = 1; while (x < 10) { document.write("<br\> Value From the While Loop is = " + x); } </script> </body> </html>

Here x is always 1 and x is always less than 10. So, it will go on execute infinite times. Now, let us add increment operator (x++) inside the while loop to the above example.
<!DOCTYPE html> <html> <head> <title> JavaScriptWhile Loop </title> </head> <body> <h1> JavaScriptWhile Loop </h1> <script> var x = 1; while (x < 10) { document.write("<br\> Value = " + x); x++; } </script> </body> </html>
Now, when it reaches 10 the condition will fail. Let us see the output
JavaScriptWhile Loop
Value = 1
Value = 2
Value = 3
Value = 4
Value = 5
Value = 6
Value = 7
Value = 8
Value = 9