JavaScript | Break and Continue

Break statement: The break statement is used to jump out of a loop. It can be used to “jump out” of a switch() statement. It breaks the loop and continues executing the code after the loop.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head
        <title>
            JavaScript Break statement
        </title>
    </head
      
    <body style="text-align:center;">
        <div>
            <h1>GeeksForGeeks</h1>
            <h2>JavaScript Break</h2>
        </div>
          
        <p id="GFG"></p>
  
        <script>
            var content = "";
            var i;
            for (i = 1; i < 1000; i++) {
                if (i === 6) { 
                    break; 
                }
                content += "Geeks" + i + "<br>";
            }
            document.getElementById("GFG").innerHTML = content;
        </script>
    </body>
</html>                                

chevron_right


Output:

Continue statement: The continue statement “jumps over” one iteration in the loop. It breaks iteration in the loop and continues executing the next iteration in the loop.

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head
        <title>
            JavaScript continue statement
        </title>
    </head
      
    <body style="text-align:center;">
  
        <div>
            <h1>GeeksForGeeks</h1>
            <h2>JavaScript continue</h2>
        </div>
          
        <p id="GFG"></p>
  
        <script>
            var content = "";
            var i;
            for (i = 1; i < 7; i++) {
                if (i === 4) { 
                    continue; 
                }
                content += "Geeks" + i + "<br>";
            }
            document.getElementById("GFG").innerHTML = content;
        </script>
    </center>
</body>
</html>                    

chevron_right


Output:

JavaScript labels: In JavaScript, the label statements are written as the statements with a label name and a colon.

Syntax:

  • break statements: It is used to jump out of a loop or a switch without a label reference while with label reference, it used to jump out of any code block.
    break labelname; 
  • continue statements: It used to skip one loop iteration with or without a label reference.
    continue labelname;

Example: This example use break label statements.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head
        <title>
            JavaScript continue statement
        </title>
    </head
       
    <body style="text-align:center;">
          
        <div>
            <h1 style="color:green;">
                GeeksForGeeks
            </h1>
              
            <h2>JavaScript break</h2>
        </div>
          
        <p id="GFG"></p>
          
        <!-- Script to use break label -->
        <script>
            var val = ["Geeks1", "Geeks2", "Geeks3",
                                 "Geeks4", "Geeks5"];
            var print = "";
  
            breaklabel: {
                print += val[0] + "<br>" + val[1] + "<br>"; 
                break breaklabel;
                print += val[2] + "<br>"+ val[3] + "<br>" + val[4]; 
            }
  
            document.getElementById("GFG").innerHTML = print;
        </script>
  
    </body>
</html>                    

chevron_right


Output:

Example: This example uses continue label.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head
        <title>
            JavaScript continue label
        </title>
    </head
      
    <body style="text-align:center">
  
        <div>
            <h1 style="color:green;">
                GeeksForGeeks
            </h1>
              
            <h2>JavaScript continue</h2>
        </div>
          
        <p id="GFG"></p>
          
        <!-- Script to use continue label -->
        <script>
            var val = ["Geeks1", "Geeks2", "Geeks3", "Geeks4"];
            var val1=["Geeks","For","Geeks"]
              
            var print = "";
              
            print += val1[0] + "<br>";
            print += val1[1] + "<br>";
            print += val1[2] + "<br>";
              
            continuelabel: {
                print += val[0] + "<br>"; 
                print += val[1] + "<br>"; 
                continue continuelabel;
                print += val[2] + "<br>"; 
                print += val[3] + "<br>"; 
            }
  
            document.getElementById("GFG").innerHTML = print;
        </script>
    </body>
</html>                    

chevron_right


Output:

Example: This example illustrates without using any label.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head
        <title>
            No label in JavaScript
        </title>
    </head
      
    <body style="text-align:center;">
  
        <div>
            <h1 style="color:green;">
                GeeksForGeeks
            </h1>
              
            <h2>JavaScript No label</h2>
        </div>
          
        <p id="GFG"></p>
          
        <script>
            var val = ["Geeks1", "Geeks2", "Geeks3", "Geeks4"];
            var val1=["Geeks","For","Geeks"]
              
            var print = "";
              
            labelloop:{
                print += val1[0] + "<br>";
                print += val1[1] + "<br>";
                print += val1[2] + "<br>";
            }
              
            print+="<br>";
              
            labelloop1: {
                print += val[0] + "<br>"; 
                print += val[1] + "<br>"; 
                print += val[2] + "<br>"; 
                print += val[3] + "<br>"; 
            }
      
            document.getElementById("GFG").innerHTML
                    = print;
        </script>
    </body>
</html>                    

chevron_right


Output:



My Personal Notes arrow_drop_up

Small things always make you to think big

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.