Label statement, according to the syntax in the book is:
Label: statement
如: begin: for (var i = 0; i < 10 ; i++ ){ alert(i); }
For a typical example, see After finishing, you will understand the application of Label: (No Label added)
var num = 0; for (var i = 0 ; i < 10 ; i++){ for (var j = 0 ; j < 10 ; j++){ if( i == 5 && j == 5 ){ break; } num++; } }
alert(num); // The loop will jump out of the j loop when i is 5 and j is 5, but it will continue to execute the i loop and output 95
Comparison of the program after using Label: (after adding Label)
var num = 0; outPoint: for (var i = 0 ; i < 10 ; i++){ for (var j = 0 ; j < 10 ; j++){ if( i == 5 && j == 5 ){ break outPoint; } num++; } } alert(num); // 循环在 i 为5,j 为5的时候跳出双循环,返回到outPoint层继续执行,输出 55
Compare the use of break and continue statements:
var num = 0; outPoint: for(var i = 0; i < 10; i++) { for(var j = 0; j < 10; j++) { if(i == 5 && j == 5) { continue outPoint; } num++; } } alert(num); //95
As can be seen from the value of alert(num), the function of the continue outPoint; statement is to jump out of the current loop and jump to outPoint ( The for loop under the label) continues to execute.
The above is the entire content of this article. I hope that the content of this article can bring some help to everyone's study or work. I also hope to support the PHP Chinese website!
For more articles related to the Label statement in Javascript, please pay attention to the PHP Chinese website!