label statementS2C Home « Statements « label statement
An identifer and statement that passes control to the code after the identifier.
Description
The label statement allows us to code a label identifier that we can pass control flow to from a break or continue statement.
- For the
breakstatement,labelcan be used with any labelled statement. - For the
continuestatement,labelcan be used withfor,whileanddo....whileloops.
Syntax
| Signature | Description |
|---|---|
label: | Allows us to code a label identifier that we can pass control flow to from a break or continue statement. |
Parameters
| Parameter | Description |
|---|---|
label | Any non reserved word identifier. |
statement | The labelled position from where to pass control. |
Examples
The code below shows an example of the label statement used with continue.
// execute the loop until loop condition is false.
for (var i=1; i<3; i++) {
atLabel:
for (var k=1; k<3; k++) {
if (k == 2) {
continue atLabel;
}
alert('The i variable is = ' + i + ' and k variable is = ' + k);
}
}
alert('left for loop');
Related Tutorials
JavaScript Intermediate Tutorials - Lesson 4 - While and Do....While Loops
JavaScript Intermediate Tutorials - Lesson 5 - For Loops