Friday, September 5, 2025
HomeLanguagesJavascriptJavaScript break and continue

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:




<!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>                                


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:




<!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>                    


Output:

JavaScript labels: In JavaScript, the label statements are written as 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.




<!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>                    


Output:

Example: This example uses continue label.




<!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>                    


Output:

Example: This example illustrates without using any label.




<!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>                    


Output:

RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11866 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS