What are the JavaScript pop-out methods?

青灯夜游
Release: 2023-01-07 11:41:23
Original
5376 people have browsed it

JavaScript jump-out methods include: 1. Break statement, which can cause the running program to immediately exit the innermost loop or exit a switch statement; 2. Continue statement, which can terminate the current loop and enter the next time Loop; 3. Return statement, which can suspend the execution of the current function and return the function value.

What are the JavaScript pop-out methods?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

A great god in the front-end industry asked me to think about a question. He gave Big-man a piece of code, as follows:

function Seriously(options) {
  // if called without 'new', make a new object and return that
  if(window === this || !(this instanceof Seriously) || this.id !== undefined) {
    return new Seriously(options);
  }
}
Copy after login

Will the return statement continue to be executed after it is executed? This is a problem that the master asked me to solve. Since return is mentioned, I will also solve the other two methods of ending the loop in JS: break and continue.

Break statement:

  • The break statement will cause the running program to immediately exit the innermost loop or exit a switch statement.
  • Since it is used to exit a loop or switch statement, this form of break statement is legal only when it appears in these statements.
  • If the termination condition of a loop is very complex, it is much easier to use the break statement to implement some conditions than to use a loop expression for all conditions.
for(var i = 519; i < 550; i++) {
  if(i == 522) {
    break;
  }
  console.log(i);
  alert(i);
  document.write(i);
}
Copy after login
  • When i = 521, exit the for loop directly. This loop will no longer be executed.
  • As for the output results, you can test it yourself.

Continue statement:

  • The continue statement is similar to the break statement. The difference is that instead of exiting a loop, it 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. Using it elsewhere will cause an error?
for(var i = 5; i >=0; i--) {
  if(i == 4 || i == 3 || i == 1) {
    continue;
  }
  console.log(i);
  alert(i);
  document.write(i);
}
Copy after login
  • When i = 4, i = 3 and i = 1, jump out of the for loop directly. Continue to execute next time.
  • As for the output results, I hope everyone can print them.

Return statement:

The return statement is used to specify the value returned by the function. The return statement can only appear in the function body, appearing anywhere else in the code will cause a syntax error!

for(var i = 1; i < 10; i++) {
  if(i == 8) {
    return;
  }
  console.log(i);
  alert(i);
  document.write(i);
}
Copy after login

Execution resultUncaught SyntaxError: illegal return statement(...)

  • The error means an illegally captured query return statement.

When the return statement is executed, even if there are other statements in the function subject, the function execution will stop!

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

In the above example, when username is empty, it will not It will be executed downwards. In some form submissions, you can also prevent the default submission method by returning false and use the Ajax submission method instead, for example:

<form id="form" onSubmit="return false">
...
</form>
Copy after login

[Related recommendations: javascript learning tutorial

The above is the detailed content of What are the JavaScript pop-out methods?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template