JavaScript Loops : While Loop

The While Loop is most basic loop in JavaScript. It is used to execute a specific block of code repeatedly as long as the given condition is true. In while loop the condition will checked first and If condition become true, execute a block of code executed. Syntax :
while (condition) {
  code to be executed
}
For example The below code will print the table of 2 :
<!DOCTYPE html>
<html>
<head>
  <title>While Loop</title>
</head>
<body>
  <p id="demo"></p>
  <script>
    var x = 2, count = 1;
    var text = '';
    while(count <= 10) {
      text = text + x*count + '<br/>';
      count++;
    }
    document.getElementById("demo").innerHTML = text;
  </script>
</body>
</html>
Output :



Next Topic :