Skip to content

JavaScript while break statement | Example code

  • by

JavaScript while loop use break statement inside a while loop to come out of the loop. The break statement to terminate a loop early in JavaScript.

For example while loop with break JavaScript

A simple example code has a break statement that terminates the while loop when i is 3.

<!DOCTYPE html>
<html>
<head>

  <script>

    var i = 0;

    while (i < 6) {
      console.log(i)

      if (i == 3) {
        console.log("Break Loop")
        break;
      }
      i += 1;
    }    

  </script>
</head>
<body>

</body>
</html>

Output:

JavaScript while break statement

A while-loop has a break, look at this simple example.

let a = 2;
while (a <100) {
    console.log(a);
    a *= 2;
    if (a===16)
        break;
}

Do comment if you have any doubts or suggestions on this JS while loop topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading