The difference between break and continue in JavaScript

break statement and continue statement


##break statement

  • Description: break statement, used to unconditionally end various loops (exit the loop) and switch.

  • Note: Generally, you need to add a conditional judgment before the break statement. In other words: when the condition is established, the loop exits.


##continue statement

    Description: End this cycle and start the next cycle. The code after continue is no longer executed.
  • Note: Generally, you need to add a conditional judgment before the continue statement.
  • <!DOCTYPE HTML>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>php.cn</title>
            <script>
                var sum = 0; 
                for(var i=1;i<=10;i++){
                    if(i==6){
                        break;
                       // continue;
                    }
                document.write(i+"  ");   
                }
               
            </script>
        </head>
        <body>
        </body>
    </html>
Note: Observe the results first, then comment out break, use continue instead, and observe the results again. It can be seen that when the conditions are met, break will jump out of the loop directly. When the loop operation is performed again, and continue is used instead, the condition is met, the current loop is jumped out, and the next loop is entered.

Continuing Learning
||
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> var sum = 0; for(var i=1;i<=10;i++){ if(i==6){ break; // continue; } document.write(i+" "); } </script> </head> <body> </body> </html>
submitReset Code