There are three ways to jump out of a loop in js: the break statement is used to exit the switch statement or loop statement, the continue statement is mainly used to interrupt the iteration in the loop, and the return statement is used to specify the value returned by the function
In object-oriented programming syntax, we often encounter the three commonly used keywords break, continue, and return. Their main function is to jump out of the loop. Next, we will introduce these three methods in detail in the article. It has a certain reference effect and I hope it will be helpful to everyone.
【Recommended course: javascript tutorial】
##break Statement
The break statement is used to exit the switch statement or loop statementWhen the break statement is used in the switch statement, the switch code block will be jumped out and the execution of the code will be terminated. When the break statement is used in a loop statement, the execution loop will be terminated and the code after the loop will be executed. Since it is used to exit the loop or switch statement, it can only be used when it appears in these statement, this form of break statement is legalIf the termination condition of a loop is very complex, it is much easier to use the break statement to implement certain conditions than to use all the conditions in a loop expression Example:<script> for(var i=1;i<=10;i++) { if(i==8) { break; } console.log(i); } </script>
Rendering:
The main function of the continue statement is to interrupt the iteration in the loop. If the specified condition occurs, then continue with the next iteration in the loop.
The continue statement is similar to the break statement, but it still exists The difference is that the continue statement does not exit a loop but starts a new iteration of the 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. If used elsewhere, it will cause errors.
<script> for(var i=1;i<=10;i++) { if(i==8) { continue; } console.log(i); } </script>
#return statement is used to specify the value returned by the function. It It can only appear within the body of a function. It will cause a syntax error if it appears anywhere else in the code. And it will terminate the execution of the function and return the value of the function
<script> for(var i=1;i<=10;i++) { if(i==8) { return; } console.log(i); } </script>
Uncaught SyntaxError: Illegal return statement(...) This sentence means Illegal captured query return statement
But if we put return in the function, it can be run
<script> function a(){ for(var i=1;i<=10;i++) { if(i==8) { return; } console.log(i); } } a(); </script>
This shows that return must be placed in the function body before it can be executed. Its function is similar to break
Summary: The above is the entire content of this article. I hope that through this article Help everyone how to break out of loop statements in JavaScript.
The above is the detailed content of Three ways to break out of loops in JS. For more information, please follow other related articles on the PHP Chinese website!