How Much Do You Know About Labeled Statements in JavaScript

Statements can be labeled for later invocation with:

label : statement;

       When you see this official sentence, you may be confused. Let me explain it to you, and you will understand.

 

label: The meaning is to allow you to customize a name for later use,
statement:是你的代码块,

来话不多说,我们上手就干,

 <script>

    let arr=[1,2,3,4,5,6,7,8,9]
      OuterLayer://定义的外层循环的名称
       for(let i of arr){
         InnerLayer://定义的内层循环的名称
         for(let g of arr){
           console.log(i+"-----------"+g)
              if(g===5) break InnerLayer; //当内层循环到5时结束名为InnerLayer的代码块
              if(i===2) break OuterLayer; //当外层层循环到2时结束名为OuterLayer的代码块
         }
       }

  </script>

Now it's very clear,

Learned, please click three times, your support is my motivation

Guess you like

Origin blog.csdn.net/qq_36131502/article/details/117928620
Recommended