Javascript-about break, continue, return statement

Source: Internet
Author: User

Js-break: The breakstatement causes the running program to immediately exit the loop that is contained in the inner layer or exit a switch statement. Since it is used to exit a loop or switch statement, this form of break statement is valid only if it appears in these statements

1  for(vari=1;i<=10;i++) { 2     if(i==8) { 3          Break; 4     } 5 Console.log (i);6 }7 //when i=8, exit for this loop directly. This loop will no longer be executed! Output Result: 12345678 varN=1;9 varn=2;Ten varN=3; One Switch(n) { A      Case1: -Console.log (1) -          Break; the      Case2: -Console.log (2) -          Break;  -     default: +Console.log (' Other ')     -}

js-continue: The continue statement is similar to the break statement. The difference is that instead of quitting 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, and will cause an error in other places!

 1  for  ( var  i=1;i<=10;i++) { 2  if  (I==8 3  continue  ;  4  }  5   Console.log (i);  6  }  7  //  when i=8, just jump out of the for loop. Continue the execution next time. Output result: 1234567910  

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

1 varFun =function () {2      for(vari=0; i<5; i++){3         if(i==2){4             return 5         }6 Console.log (i)7     }8 }9 TenFun ();//Output 0 1

Javascript-about break, continue, return statement

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.