Parse the tag statement in JavaScript

Source: Internet
Author: User

A label in Javascript is an identifier. A tag can be renamed with a variable. It is an independent syntax element (neither a variable nor a type). Its function is to identify "labeled statement )"

Label Declaration
A label can be declared before any statement or block to make the statement or block "labeled )".
Copy codeThe Code is as follows:
MyFun1 ();
Label2 :{
Var I = 1, j = 2;
Var k = I + j;

Note:When a label is followed by multiple consecutive statements, only the first statement is tagged.
Although GOTO is a reserved keyword of Javascript, there is no GOTO statement in it. In addition to GOTO, there are three other keywords in Javascript that can change the process of the program: break, continue, and return. Here, break and continue can be used with labels.

Break and tag
Break is usually used to jump out of the for, while loop, and switch statements. By default, the break clause acts on the innermost layer of a loop statement or the entire switch statement. Therefore, it does not have to specify the scope of the interrupt statement. However, the break clause has an extended syntax to indicate its scope.

Break my_label;
In addition to jumping out of loops and switch branches, you can also jump out of the labeled statement
Copy codeThe Code is as follows:
Var str = prompt ('Please input a string', '123 ');

My_label :{
If (str & str. length <10 ){
Break my_label:
Str = str. substr (str. length-10 );

Alert (str );

Continue and label
Continue is only meaningful to loop statements, so it can only act on for,... In, while, And do... While. By default, it indicates to stop the current loop and jump to the start of the next loop iteration.

You can also add a label after the continue, which indicates that it is aborted from the inside of the loop body and continues to run at the label indication, the statement indicated by this label must be a loop statement containing this continue.
For example:
Copy codeThe Code is as follows:
For (var j = 0; j <5; j ++)
If (j = 2) continue loop;
Document. write ("loop:" + j + );

In the above example, continue + label does not reflect the special function of label. In fact, the label can be removed completely, and the effect is the same. Let's look at another example.
Copy codeThe Code is 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 directly jump to the outer loop is what it means.

Related Article

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: 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.