On the difference _javascript skills of break, continue and return in JavaScript

Source: Internet
Author: User

Break

function Mybreak () {for
(var i = 0; i < 5; i++) {
if (i = = 3) {break
;
}
Console.log (i);
}
}
Mybreak ();

Output:

0
1
2

Break: Jump out of the current loop, start from outside the current loop, ignore any other statements in the loop body and test for cyclic conditions. It can only jump out of the loop, if your cycle is nested loops, then you need to follow your nesting level, and gradually use the break to jump out.

Continue

function Mycontinue () {for
(var i = 0; i < 5; i++) {
if (i = = 3) {
continue;
}
Console.log (i);
}
}
Mycontinue ();

Output:

0
1
2
4

Continue: Terminates the current one cycle process, it does not jump out of the loop, but continues to judge the loop condition execution statement.

Only one procedure in the loop can be ended, but the loop cannot be terminated.

Return

function Myreturn () {for
(var i = 0; i < 5; i++) {
if (i = = 3) {return
i;
}
Console.log (i);
}
}
var s = Myreturn ();
Console.log ("s:" + s);

Output:

0
1
2
S:3

Return: Exits from the current method, returns to the statement of the method being invoked, and continues execution.

Summarize

1. Function of Return statement

(1) Return is exited from the current method, returned to the statement of the method that called, and continues execution

(2) return returns a value to the statement that calls the method, the data type of the return value must match the type of the return value in the declaration of the method, and you can use coercion type conversion to be a data type consistent

(3) Return when the method description uses a void declaration to empty a type, use this format and return no value.

2, the function of the break statement

(1) The break statement can only be used in the loop body and in the switch statement body.

(2) When a break appears in a switch statement body in the loop body, the function only jumps out of the switch statement body.

(3) When the break appears in the loop body, but is not in the switch statement body, after the execution of the break, jump out of the loop body of the layer.

(4) In the circular structure, the break statement is applied to make the process jump out of the loop of this layer, thus ending the cycle in advance

3, continue statement function

(1) The general form of continue statement is: continue;

(2) Its function is to end this cycle, that is, skip the remaining statements in the body of this cycle, and then again the condition of the cycle of determination.

(3) Note: Executing the CONTINUE statement does not terminate the entire loop. In the while and do-while loops, the Continue statement causes the process to skip directly to the test portion of the loop control condition, and then decide whether the loop continues.

(4) in the For loop, after encountering the continue, skip the remaining statements in the loop body, evaluate the expression 3 in the For statement, and then perform the conditional test of expression 2.

Finally, determine if the For loop executes based on the value of expression 2. In a circular body, regardless of the statement element in which the continue is used, it is performed according to the above function, which differs from the break

The above is a small set to introduce the JavaScript in the break, continue and return of the difference, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.