JavaScript Break and Continue

previous next


There are two special statements that can be used inside loops: break and continue.


JavaScript Break and Continue Statements

There are two special statements that can be used inside loops: break and continue.

Break

The break command will break the loop and continue executing the code that follows after the loop (if any).

Example

<html>
<body>
<font face="Verdana" size=2>


<script type=
"text/javascript">
  var i=0
  for (i=0;i<=10;i++)
  {
    if(i==5)break
    document.write("The number is: " + i + "<br>")
  }
</script>

</font>
</body>

</html>

Result

The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4

 

Continue

The continue command will break the current loop and continue with the next value.

Example

<html>
<body>
<font face="Verdana" size=2>


<script type=
"text/javascript">
  var i=0
  for (i=0;i<=10;i++)
  {
    if(i>4 && i<8)continue
    document.write("The number is: " + i + "<br>")
  }
</script>

</font>
</body>
</html>

Result

The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 8
The number is: 9
The number is: 10

 


Try it

To see how HTML and JavaScript work, you can only push the submit button, or you can make your own HTML and JavaScript code.

           

 


previous next