This article shares the label description of javascript syntax. This article shares the label description of javascript syntax.
The JavaScript language allows the statement to be preceded by a label, which is equivalent to a positioning character used to jump to any position of the program. The label format is as follows.
A tag can be any identifier, but cannot be a reserved word. A statement can be any statement.
Labels are usuallybreakStatement andcontinueStatement to jump 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('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 dual-loop block,breakThe command is followedtopLabel (note,topWithout quotation marks). When conditions are met, the double-layer loop is directly exceeded. IfbreakIf no label is used after a statement, the system can only jump out of the inner loop and enter the next outer loop.
continueStatements 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 code above,continueThe command is followed by a tag name. When the condition is met, the current loop is skipped and the next round of outer loop is directly entered. IfcontinueIf no label is used after a statement, the statement can only enter the next round of inner loop.
Note:
breakStatement andcontinueStatements have a jump function, so that the Code is not executed in the original order.
breakStatements are used to exit code blocks or loops.
continueThe statement is used to terminate the current loop immediately. It returns the header of the Loop Structure and starts the next loop.
The above is the detailed description of the label of javascript syntax. For more information, see other related articles in the first PHP community!