JavaScript QuickStart-ecmascript Statements

Source: Internet
Author: User

JavaScript statements (if, for, in, Do...while, while, break, continue, switch)

One, if statement
if(conditionstatement1 else Statement2

1. If ... else .....

if (2>1) {  alter ("2 greater than 1")  } else {  alert ("2 less than 1")  }

  

2. If....else If ... else ...

<script>var A=3;var b=2if (a>b) {alert (' A greater than B ');} else if (a<b) {alert (' A is less than B ');} Else{alert (' a equals B ');} </script>

  

Second, circular statement 1, Do...while
<script>     var a=0;    do{        a+=1;        Console.log (a);        } while (A<10) </script>

  

2. While
<script>    b=10;    while (b>0) {        console.log (b);        b-=1;    } </script>

  

3. For
<script> for  (var a=0;a<10;a++) {        console.log (a);    } </script>

  

4, for...in (not recommended)
<script> var b=[1,2,3,4,5] for    (i in B) {        console.log (b[i]);    } </script>

  

Third, switch

Syntax: Judge the expression Expresssion and the following situation, compare, and then immediately exit the loop.

switch (expression) case  value:statement;    break;  Case Value:statement;    break;  Case Value:statement;    break;  Case Value:statement;    break;  Case Value:statement;    break;  default:statement; Note: Each statement has a break keyword! In fact, the switch is the same as if, but switch uses break, the efficiency will be higher than if. The number of judgements is less than if (except in extreme cases).

  

For example: Note: switch can perform string comparisons.

<script>    var a= ' string '    switch (a) {case        ' AAA ': Console.log (' aaa ');        break;        Case ' string ': Console.log (' I am a string ');        break;        Case ' test ': console.log (' Test ');        break;        Default:console.log (' default ');    } </script>

  

Iv. break and Continue

The break statement can be used to jump out of this loop.

After the continue statement jumps out of the loop, the code after the loop continues to execute (if any)


Example: Continue
<script>for (var i=0;i<10;i++) {        if (i==5) {        continue;        } else{          Console.log (i);                    } </script> Results: 12346789
Break
<script>for (var i=0;i<10;i++) {        if (i==5) {break        ;        } else{          Console.log (i);                    } </script> Results: 1234

  

Five, exception statement

try {    //This piece of code runs from top to bottom, where any one of the statements throws an exception the code block ends running}catch (e) {    ///If an exception is thrown in a try code block, the code in the catch code block is executed.    //e is a local variable that is used to point to an Error object or other thrown object}finally {//Whether or not the code in the     try is thrown (even if there is a return statement in the try code block), the finally code block is always executed. }

  

JavaScript QuickStart-ecmascript Statements

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.