break语句用于尽早退出循环,使之脱离封闭的花括号。
您可以尝试运行以下代码,以了解如何使用break语句退出循环
<html> <body> <script> var x = 1; document.write("Entering the loop<br /> "); while (x < 20) { if (x == 5) { break; // breaks out of loop completely } x = x + 1; document.write( x + "<br />"); } document.write("Exiting the loop!<br /> "); </script> </body> </html>