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

JavaScript syntax label (label) description

高洛峰
高洛峰 Original
2017-03-12 11:49:12 2391browse

Share this articlejavascript syntaxLabel (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


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 thebreakstatement and thecontinuestatement to break out of a specificloop.


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


The above code is a double loop block, thebreakcommand is followed bytoptag (note thattopdoes 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 thebreakstatement, you can only break out of the inner loop and enter the next outer loop. The

continuestatement 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('i=' + i + ', j=' + 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


In the above code, there is a label name after thecontinuecommand. 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 thecontinuestatement, you can only enter the next round of inner loop.

Note: Both the

breakstatement and thecontinuestatement have a jump function, which allows the code to be executed out of the existing order. The

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

continuestatement 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!

Statement:
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