Break statement:

  • The break statement causes the running program to immediately exit the innermost loop or a switch statement.
  • Since it is used to exit loops or switch statements, this form of break is legal only if it appears in those statements.
  • If the termination conditions of a loop are very complex, it is much easier to implement some of the conditions using a break statement than to express all of the conditions in a single loop.
for(let i = 0; i < 50; i++) {
    if(i == 5) {
        break;
    }
    console.log(i);
}
Copy the code

When I is equal to 5, we just exit the for loop. The loop will no longer be executed.

The Continue statement:

  • A continue statement is similar to a break statement. The difference is that instead of exiting a loop, it starts a new iteration of the loop.
  • A continue statement can only be used inside a loop of a while, do/while, for, or for/in.
for(var i = 5; i >=0; i--) {
    if(i == 4 || i == 3 || i == 1) {
        continue;
    }
    console.log(i);
}
Copy the code

When I = 4, I = 3, and I = 1, jump out of the for loop. Continue next time. As for the output result, I hope you can print it.

Return statements:

  • The return statement is used to specify the value returned by the function.
  • A return statement can only appear inside a function, anywhere else in the code causing a syntax error!
for(var i = 1; i < 10; i++) {
    if(i == 8) {
        return;
    }
    console.log(i);
    alert(i);
    document.write(i);
}
Copy the code

Uncaught SyntaxError: illegal return statement(…) Error means an illegally captured query return statement. When a return statement is executed, function execution stops even if there are other statements in the function subject!

<script type="text/javascript">
    if(username == "") {
        alert("please input your username: ");
        return false;
    } else if (qq == "") {
        alert("please input your qq number: ");
        return false;
    }
</script>
Copy the code

In the above example, if username is null, no further execution will be performed. In some form submissions, you can also return false to prevent the default submission and use Ajax submission instead, for example:

<form id="form" onSubmit="return false">... </form>Copy the code

The original link: blog.csdn.net/XXJ19950917…