JS Supplemental trivia point (Continue,break,ruturn)

Source: Internet
Author: User

1.continue,break,ruturn

The eg:1-100 and

$ (function  () {    $ ("#hello"). Click (function  ()    {        var iNum = 0;          for (var i = 1; i < 101; i++)        {            + = i;         }        alert (iNum)     ;});

The result is: 5050

Change to break to see the results

$ (function  () {    $ ("#hello"). Click (function  ()    {        var iNum = 0;          for (var i = 1; i < 101; i++)        {            if (i = = 5)            {                break;            }             + = i;        }        alert (iNum);     });

The result is: 10

Conclusion One: Break: Jump out of the entire loop body

How much is continue look at the results?

1$(function ()2 {3$ ("#hello"). Click (function ()4     {5         varINum = 0;6          for(vari = 1; I < 101; i++)7         {8             if(i = = 5)9             {Ten                 Continue; One             } AINum + =i; -         } - alert (iNum); the     }); -});

The result is: 5045, (executed except 5)

Conclusion Two: Continue skips the current condition of the loop

There are two ways to use return:

First use: Change to return to see what the result is?

The result is: no result, return End method body, jump directly outside the method body, so cannot print

The second usage of return: A method that returns a value

1$(function ()2 {3$ ("#hello"). Click (function ()4     {5         varINum = 0;6          for(vari = 1; I < 101; i++)7         {8             if(i = = 5)9             {TenI=A (i); One             } AINum + =i; -         } - alert (iNum); the     }); -     //the second use of return is that a method returns a value -     functionA (i) -     { +i + = 5; -         returni; +     } A});

The result is: 5015

Process parsing:

Conclusion Three: Return two usages, the first end of the entire method body, and the second, a method that returns a value

JS Supplemental trivia point (Continue,break,ruturn)

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.