Differences between break, continue, and return in JavaScript: javascriptbreak

Source: Internet
Author: User

Differences between break, continue, and return in JavaScript: javascriptbreak

Break

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

Output:

0
1
2

Break: directly jumps out of the current loop, starts execution from outside the current loop, and ignores any other statements and cyclic condition tests in the loop body. It can only jump out of one loop. If your loop is a nested loop, you need to follow your nested hierarchy and gradually use 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: Terminate the current loop process. Instead of jumping out of the loop, the system continues to judge the statements executed by the loop conditions.

Only one process in the loop can be ended, but the loop cannot be terminated to continue.

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: Exit from the current method, return to the statement of the method called, and continue execution.

Summary

1. Functions of return statements

(1) return exits from the current method, returns to the statement of the method called, and continues to execute

(2) return returns a value to the statement that calls this method. The data type of the returned value must be the same as the type of the returned value in the method declaration. You can use forced type conversion to make the data type consistent.

(3) return when void is used to declare that the return type is null in the method description, this format should be used without returning any value.

2. Roles of the break statement

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

(2) When the break appears in the switch statement body of the loop body, it 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, it jumps out of the loop body at the current layer after the break is executed.

(4) In the loop structure, the break statement is used to bring the process out of the cycle body of the Current layer, so that the cycle of the current layer is ended in advance.

3. Functions of the continue statement

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

(2) its function is to end this cycle, that is, to skip the remaining statements in the cycle body that have not been executed, and then judge the conditions of the cycle again.

(3) Note: The execution of the continue statement does not terminate the entire loop. In the while and do-while loops, The continue statement causes the process to jump directly to the test section of the loop control condition, and then determines whether the loop continues.

(4) In a for loop, when a continue is encountered, skip the remainder statement in the loop body and evaluate the value of "expression 3" in the for statement, then perform the conditional test of expression 2,

Finally, the for loop execution is determined based on the value of expression 2. In a loop, No matter what statement the continue is, it will be executed according to the above function, which is different from break.

The above section describes the differences between break, continue, and return in JavaScript. I hope it will help you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.