JavaScript tutorial:
Labeled statement

 

The labeled statement provides an identifier for a statement.

Syntax

label :
    statement

Labeled statement syntax has these parts:

Part

Description

label

A unique identifier used when referring to the labeled statement.

statement

The statement associated with label. May be a compound statement.

Return value

None

Example

Labels are used by the break and continue statements to specify the statement to which the break and continue apply.

In the following statement the continue statement uses a labeled statement to create an array in which the third column of each row contains and undefined value:

function labelTest()
{
var a = new Array();
var i, j, s = "", s1 = "";
Outer:
for (i = 0; i < 5; i++)
{
Inner:
for (j = 0; j < 5; j++)
{
if (j == 2)
continue Inner;
else
a[i,j] = j + 1;
}
}
for (i = 0;i < 5; i++)
{
s = ""
for (j = 0; j < 5; j++)
{
s += a[i,j];
}
s1 += s + "\n";
}
return(s1)
}
document.write(labelTest());

To run the code, paste it into JavaScript Editor, and click the Execute button.

See also: break statement, continue statement