Home > Web Front-end > JS Tutorial > body text

Three ways to break out of loops in JS

清浅
Release: 2019-03-07 14:29:13
Original
8222 people have browsed it

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

Three ways to break out of loops in JS

##break Statement

The break statement is used to exit the switch statement or loop statement

When 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 legal

If 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>
Copy after login

Rendering:

Three ways to break out of loops in JS

##continue statement

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>
Copy after login

Figure:


Three ways to break out of loops in JS

return statement:

#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>
Copy after login

The execution result is:

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>
Copy after login

Rendering:

Image 061.pngThis 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template