Home > Web Front-end > JS Tutorial > JavaScript syntax label (label) description

JavaScript syntax label (label) description

高洛峰
Release: 2017-03-12 11:49:12
Original
2463 people have browsed it

Share this articlejavascript syntax Label (label) description

JavaScript language allows, there is a label (label) in front of the statement, which is equivalent to the locator, used to jump to Anywhere in the program, the format of the label is as follows.


label:
  statement
Copy after login


The label can be any identifier, but it cannot be a reserved word, and the statement part can be any statement. The

tag is usually used in conjunction with the <a href="//m.sbmmt.com/wiki/130.html" target="_blank">break</a> statement and the <a href="//m.sbmmt.com/wiki/131.html" target="_blank">continue</a> statement to break out of a specific loop.


top:
  for (var i = 0; i < 3; i++){
    for (var j = 0; j < 3; j++){
      if (i === 1 && j === 1) break top;
      console.log(&#39;i=&#39; + i + &#39;, j=&#39; + j);
    }
  }
// i=0, j=0
// i=0, j=1
// i=0, j=2
// i=1, j=0
Copy after login


The above code is a double loop block, the break command is followed by top tag (note that top does not need to be quoted), when the conditions are met, the double-layer loop will be jumped out directly. If no label is used after the break statement, you can only break out of the inner loop and enter the next outer loop. The

continue statement can also be used with tags.


top:
  for (var i = 0; i < 3; i++){
    for (var j = 0; j < 3; j++){
      if (i === 1 && j === 1) continue top;
      console.log(&#39;i=&#39; + i + &#39;, j=&#39; + j);
    }
  }
// i=0, j=0
// i=0, j=1
// i=0, j=2
// i=1, j=0
// i=2, j=0
// i=2, j=1
// i=2, j=2
Copy after login


In the above code, there is a label name after the continue command. When the conditions are met, it will be skipped. The current loop directly enters the next round of outer loop. If no label is used after the continue statement, you can only enter the next round of inner loop.

Note: Both the

break statement and the continue statement have a jump function, which allows the code to be executed out of the existing order. The

break statement is used to break out of a code block or loop. The

continue statement is used to immediately terminate this cycle, return to the head of the loop structure, and start the next cycle.

The above is the detailed content of JavaScript syntax label (label) description. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template