js中break,continue,return

Among the three keywords of break, continue and return, break and continue are together, and return is the function return statement, but the return also stops the function at the same time.

break和continue

The effect of using the break statement alone is to immediately exit the innermost loop or swith statement

break: Since it is used to exit a loop or switch statement, this form of break statement is legal only if it appears in these statements.

Example:

for(var i=1;i<=10;i++)  

if(i==8)

{

    break; 

document.write(i); 

} //Output result: 1234567

When i=8, exit the for loop directly. This loop will no longer be executed!

continue: Similar to the break statement. But instead of exiting the loop, it instead executes the next loop. The continue statement can only be used in the loop body of a while statement, do/while statement, for statement, or for/in statement. Any other use will cause an error!

Example:

for(var i=1;i<=10;i++) 

{

if(i==6) continue;

 document.write(i); // A piece of text is output to the web document as a    variable

} //Output result: 1234578910

When i=6, jump out of this for loop directly. Continue to execute next time.

 

The third return statement:

The return statement is used to specify the value returned by the function.

The scope of the return statement can only appear in the body of the function, and any other place in the code will cause a syntax error!

1. Return control and function result,

The syntax is: return expression; the statement ends the execution of the function, returns the calling function, and uses the value of the expression as the result of the function

 2. Return to control,

No function result, the syntax is: return;

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326864100&siteId=291194637