Parsing the label statement in JavaScript basics

Source: Internet
Author: User
Tags goto
The label (label) in JavaScript is an identifier. A label can have the same name as a variable, which is a separate syntactic element (neither a variable nor a type), and its function is to identify the "labeled statement"

Label declaration
A label can be declared in front of any statement, or before a block of statements, so that the statement or block of statements is "tagged (labeled)".
Label1:
Copy Code code as follows:

MyFun1 ();
label2:{
var i = 1, j = 2;
var k = i + j;
}

Note: when more than one statement is followed by a label (label), only the first statement is tagged
Although Goto is a reserved keyword for JavaScript, there is no goto statement. In addition to Goto, there are three other keywords in JavaScript that can change the process of the program: Break,continue and return. Where break and continue can be used with labels (label).

Break and label
A break is often used to jump out of a for, while loop, and jump out of a switch statement. By default, the break clause acts on the inner layer of the loop statement, or the entire switch statement, so it does not have to specifically specify the scope of the interrupt statement. The break clause, however, has an extended syntax to indicate its scope.

Break My_label;
In addition to being able to jump out of loops and switch branches, you can also jump out of the tabbed statement (labeled statement) inside
Copy Code code as follows:

var str = prompt (' Please input a string ', ' 1234567890 ');

My_label: {
if (str && str.length < 10) {
Break My_label:
}
str = STR.SUBSTR (STR.LENGTH-10);
}

alert (str);

Continue and labels
Continue only makes sense for circular statements, so it can only be used internally for the statements of for, for...in, while, and Do...while. By default, it indicates that the current loop is stopped and jumps to the beginning of the next loop iteration.

The continue can also be followed by a label (label), which indicates that it is aborted from within the loop body and continues to the label indicating that the statement must be a circular statement containing this continue.
For example:
Loop
Copy Code code as follows:

for (var j = 0; J < 5; J + +)
{
if (j = = 2) continue loop;
document.write ("Loop:" + j +);
}

The above example continue + label does not show the special role of label, in fact, can completely remove the label, the effect is the same. Let's look at one more example
Copy Code code as follows:

document.write ("Entering the loop!<br/>");
Outerloop://This is the label name
for (var i = 0; i < 3; i++)
{
document.write ("Outerloop:" + i + "<br/>");
for (var j = 0; J < 5; J + +)
{
if (j = = 3) {
Continue Outerloop;
}
document.write ("Innerloop:" + j + "<br/>");
}
}
document.write ("Exiting the loop!<br/>");

Using the continue label to jump directly to the outer loop is what it means.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.