Computer >> Computer tutorials >  >> Programming >> Javascript

What is while loop statement in JavaScript?


The most basic loop in JavaScript is the while loop. 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

The syntax of while loop in JavaScript is as follows −

while (expression){
   Statement(s) to be executed if expression is true
}

Example

You can try to run the following example to implement while loop

Live Demo

<html>
   <body>
      <script>
         var count = 0;
         document.write("Starting Loop ");
         
         while(count < 10){
            document.write("Current Count : " + count + "<br/>");
            count++;
         }
         document.write("Loop stopped!");
      </script>
   </body>
</html>