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
break
statement,label
can be used with any labelled statement. - For the
continue
statement,label
can be used withfor
,while
anddo....while
loops.
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