What is while loop statement in JavaScript?

JavascriptWeb DevelopmentFront End Technology

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>
raja
Published on 19-Jan-2018 11:03:44
Advertisements