JavaScript jump statements

Source: Internet
Author: User
Tags tag name

The jump statement, as you can see from the name, allows JavaScript execution to jump from one location to another. The break statement is a jump to the end of a loop or other statement, and the continue statement terminates execution of the loop and begins execution of the next loop. Statements in JavaScript can be named or tagged, and break and continue can identify target loops or other statement tags.

The return statement allows the interpreter to jump out of the function body and provide the return value of the call. The throw statement triggers or throws an exception, which is used with the try/catch/finally statement, which specifies the code logic to handle the exception. This is a complex jump statement, and when an exception is thrown, the program jumps to the nearest closed exception handler, which can be in the same function or in a higher-level call stack.

(1) Label statement

A statement is a label that can be labeled by the identifier and colon before the statement.

Identifier:statement;

By defining a label for a statement, you can refer to the statement anywhere in the program by tag name. You can also define labels on multiple statements, although they are more useful when defining labels for a statement block, such as loops and conditional judgments. By defining a tag name for the loop, you can use break and continue inside the loop body to exit the loop or jump directly to the beginning of the next loop. Break and continue are the only statements in JavaScript that can use statement labels.

The continue statement uses this tag:

Mainloop:while (token! = null) {//Ignore the code here continue Mainloop; Jump to Next loop//Ignore code here}

The identifier used for labeling here must be a valid JavaScript identifier and cannot be a reserved word.

The namespace of the tag is different from the namespace of the variable or function, so you can use the same identifier as the statement label and as the statement label and as the variable name or function name.

A statement tag is defined only within the statement that it works (and, of course, in its clauses). A statement label cannot have the same name as the statement label inside it, but it is possible to have a statement label with the same names if the two code segments are not nested with one another.

A labeled statement can also have a label, meaning that any statement can have many labels.

(2) Break statement

The use of the break statement alone immediately exits the inner loop or switch statement. Its syntax is as follows:

Break

Because it enables loops and switch statements to exit, this form of break is only legal if it appears in such statements.

In a loop, you can use break to exit early, for whatever reason, as long as you do not want to continue with the entire loop. When the loop termination condition is very complex, it is much simpler to use the break statement in the function body to make these conditional judgments than to write the complex termination condition directly in the loop expression.

In the following example, the loop iterates through the entire array element to find a particular value, exits the loop normally when the entire array traversal is complete, and exits the loop using the break statement if the array element to find is found.

for (var i = 0; i<a.length; i++) {if (a[i] = = target) break;}

JavaScript also allows the break keyword to follow a statement label (only identifiers, no colons):

Break LabelName;

When break is used with a tag, the program jumps to the end of the block of statements identified by the tag, or terminates the execution of the closed statement immediately. A syntax error occurs when there are no closed statement blocks that specify the label to use for break. When this form of break statement is used, the tagged statement should not be a loop or switch statement, because break can "jump out" of any closed block of statements. The statement here can be a set of statements enclosed in curly braces, using the same label to represent the set of statements.

Cannot wrap between the break keyword and labelname. Because JavaScript can give the statement auto-fill the whole province slightly off the semicolon, if the break keyword and the label has a newline, the JavaScript interpreter will assume that you are using the cut without the simplest form of the label, and therefore will fill the full number after break.

When you want to break out of a non-nearest loop body or switch statement, you'll use a tagged break statement, here's an example:

Var matrix = getdata ();     //gets a two-dimensional array from somewhere//sums all the elements in the matrix var sum =  0,success = false;//starts with the tag name to exit the program Compute_sum: if (matrix) {    for ( var x =0;x<matrix.length;x++) {    var row = matrix[x];         if (!row)  break compute_sum;             for (var y = 0; y<row.length; y++) {                 var cell =  row[y];                 if (IsNaN (cell))  break compute_sum;                 sum += cell;             }&Nbsp;   }    success = true;} The break statement jumps to this//if you arrive at this point in the success == false condition, it means we have an error in the matrix we gave, or else we will sum all the elements in the matrix.

Finally, it is important to note that the break statement does not have control over the bounds of the function, regardless of the tag. For example, for a labeled function definition statement, you cannot jump to the outside of the function from within the function through this tag.




JavaScript jump statements

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