while Loop in JavaScript

The while loop in JavaScript, is used to continue executing some block of code, until the given condition evaluates to be false. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <script>
      let num = 5, i = 1;
      while(i<=10)
      {
         document.write(num, " * ", i, " = ", num*i, "<BR>");
         i++;
      }
   </script>
   
</body>
</html>
Output

JavaScript while Loop Syntax

The syntax of while loop in JavaScript, is:

while(condition) {
   // body of the loop
}

Note - Do not forgot to update the loop variable. Otherwise the loop may go for infinite execution.

JavaScript Online Test


« Previous Tutorial Next Tutorial »