什么是 break 关键字
- break 关键字可以用于 switch语句和循环结构中
- 在 switch 语句中 break 关键字的作用是立即结束当前的 switch语句
- 在循环结构中 break 关键字的作用也是立即结束当前的循环结构
break 关键字的注意点
- break 关键字后面不能编写任何的语句, 因为永远执行不到
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script type="text/javascript">
        let num = 123;
        switch (num) {
            case 123:
                console.log("123");
                break;
                // 永远执行够不到
                console.log("break后面的语句");
            case 124:
                console.log("124");
                break;
            default:
                console.log("other");
                break;
        }
    </script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script type="text/javascript">
        let num = 1;
        while (num <= 10) {
            console.log("发射子弹" + num);
            num++;
            break;
            // 永远执行够不到
            console.log("break后面的语句");
        }
    </script>
</head>
<body>
</body>
</html>
- 如果在循环嵌套的结构中, break 结束的是当前所在的循环结构
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script type="text/javascript">
        for (let i = 0; i < 5; i++) {
            console.log("外面的循环结构" + i);
            for (let j = 0; j < 5; j++) {
                console.log("里面的循环结构-----" + j);
                break;
            }
        }
    </script>
</head>
<body>
</body>
</html>
