JavaScript While Loop Example

JavaScript While Loop check condition and execute while block for a specify number of times, until while condition become false.

Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>JavaScript while loop</title>
</head>
<body>
<pre>  <!-- Use pre element for work document.writeln() method -->
<script>
    number = 1;
    while (number <= 10) {
        document.writeln(number + " times");
        number++;
    }
    document.writeln("Total " + (number - 1) + " times loop repeat");
</script>
</pre>
</body>
</html>

Run it...   »

Example Result