JavaScript Advanced Programming Notes: Continue/break and return

Source: Internet
Author: User

Several features of the return statement:
(1) The return statement can only be applied to the function body, and anywhere else in the code will cause a syntax error
(2) The return statement is used to specify the value returned by the function
(3) return False to prevent the submission of the form or continue to execute the following code, in general, to prevent the execution of the default behavior

Example 1 of return:

function returnFn() {    for(var i = 0; i < 10; i++) { if(i == 5) { return i; } console.log(i); }}console.log("===", returnFn());// 0 1 2 3 4 === 5

Example 2 of return:

<ahref="http://www.rcsx.org" onclick="return fn ()" > Click  </a><script type= "text/javascript" >function fn() {location.href="http://www.sina.com.cn"; return false;} </script>               

Click the hyperlink will jump to Sina and will not jump to Baidu, if not renturn false will jump to Baidu. Because return false; the default behavior is blocked

Several common uses of return:
(1) Cancel the default behavior (refer to the previous example)
(2) Chain-type programming

var a = {B:functionBB) {console.log (BB) return this;}, c: function ( cc) {console.log (cc) return Span class= "Hljs-keyword" >this; }, d: function ( dd) {console.log (DD) return Span class= "Hljs-keyword" >this; }}A.B (1). C (2). D (3); //1//2//3// Object {}                

(3) Closures (later chapters are explained in detail)

Continue and Break statements

The break and continue statements are used to precisely control the execution of code in a loop. Where the break statement exits the loop immediately, forcing the statement following the loop to continue execution. While the continue statement exits the loop immediately, it resumes from the top of the loop after exiting the loop

Example of break:

function breakFn() {    for(var i = 0; i < 10; i++) { if(i == 5) { break; } console.log(i); }}breakFn();// 0 1 2 3 4

Break: Jumps directly out of the current loop, executes from outside the current loop, ignoring any other statements and loop condition tests in the loop body. It can only jump out of a layer of loops, if your loop is nested loops, then you need to follow your nesting level, the eight-ring theater gradually use break to jump out.

Examples of continue:

function continueFn() {    for(var i = 0; i < 10; i++) { if(i == 5) { continue; } console.log(i); }}continueFn();// 0 1 2 3 4 6 7 8 9

Continue: Terminates the current loop process, which does not jump out of the loop, but continues to judge the loop condition execution statement. You can only end a process in a loop, but you cannot terminate the loop to continue.

JavaScript Advanced Programming Notes: Continue/break and return

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.