The break and continue Statements

The break statement exits the loop. The continue statement exits the current loop.

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
    
        var num = 0; 
        for (var i=1; i < 10; i++) { 
            if (i % 5 == 0) { 
                break; 
            } 
            num++; 
        } 
        document.writeln(num); //4 

       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo
 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
    
        var num = 0; 
        for (var i=1; i < 10; i++) { 
            if (i % 5 == 0) {
               continue;
            } 
            num++; 
        } 
        document.writeln(num); //8 

       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

breaks to outer most loop

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
    

        var num = 0; 
        
        outermost: 
        for (var i=0; i < 10; i++) { 
            for (var j=0; j < 10; j++) { 
                if (i == 5 && j == 5) {
                    break outermost; 
                } 
                num++;
            } 
        } 
        document.writeln(num); //55 
       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

continue to outer most level:

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
    
        var num = 0; 
        
        outermost: 
        for (var i=0; i < 10; i++) { 
            for (var j=0; j < 10; j++) { 
                if (i == 5 && j == 5) {
                     continue outermost;
                } 
                num++; 
            } 
        } 
        document.writeln(num); //95 

       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    Language Basics  

Statements:
  1. The if Statement
  2. The do...while Statement
  3. The while Statement
  4. The for Statement
  5. The for-in Statement
  6. Labeled Statements
  7. The break and continue Statements
  8. The with Statement
  9. The switch Statement