JS Elevation 3. Basic Concept (5) statement

Source: Internet
Author: User

1.if statements

2.do-while statement: post-test Loop statement, The code of the loop body executes at least once.

3.while statement: pre-test Loop Statement.

4.for statement: pre-test Loop Statement.

  Note: There is no block-level scope in ecmascript, so variables defined inside the loop can also be accessed externally.

eg

1 var count=10; 2      for (var i=0;i<count;i++) {3        alert (i); 4     }5     Alert (i);

The effect of the above code is to pop the warning box from 0 to 9, which is the result of the for statement loop, and finally the warning box showing 10, which is the effect of the statement outside the for loop, which indicates that variables defined inside the loop can also be accessed externally.

5.for-in statements

The for-in statement is a precise iterative statement that can be used to enumerate the properties of an object,

Syntax: for (proerty in experssion) statement

eg

1  for (var in window) {2        document.write (propname); 3     }

  The above example uses the for-in loop to display all the properties of the Window object in the Bom. Once per loop, a property name that exists in the Window object is assigned to the variable propname until all the properties in the object are enumerated again.

The properties of the ECMAScript object are not in Order. therefore, the order of the property names that are output through the for-in loop is unpredictable, specifically, All properties are returned once, but the order of return may vary by browser.

Note that if the variable value of the iterated object is null or the undefined,for-in statement throws an Error. ECMAScript5 corrects this behavior by not throwing an error in this case, but simply not executing the loop body. For maximum compatibility, It is recommended to detect that the value of the object is not null or undefined before using the for-in loop.

W3c:

The for...in statement is used to iterate over an array or the properties of an object (looping over a group or an object's properties).

JavaScript for...in Statements

The for...in statement is used to loop an array or an object's Properties.

The code for the for ... in loop executes once for each time, and the elements of the array or the properties of the object are manipulated once .

Grammar:

forinObject) {    Execute Code here}

A variable is used to specify a variable, which can be an array element or a property of an object.

eg

1 var New Array () 2 mycars[0] = "Saab"3 mycars[1] = "Volvo"4 mycars[2] = "BMW"56for
     (var in mycars)7{8 document.write (mycars[x] + "<br/>")9 }

6.label Statements use the Label statement to add tags to your code for future Use. Syntax: label:statement  

Eg: refer to the following example of break and Cuntinue Statements.

7.break and Continue statements

The break and continue statements are used to precisely control the execution of code in a loop. Break exits the loop immediately, forcing continuation of the statement following the loop, while the continue statement exits the loop immediately, but exits the loop and regrets that it continues to execute immediately from the top of the Loop.

eg

1 outermost:2   for(vari=0;i<10;i++){3      for(varj=0; j<10;j++){4         if(i==5&&j==5){5              breakoutermost;6         }7num++;8     }9  }TenAlert (num);

Change to Continue:

1 outermost:2   for(vari=0;i<10;i++){3      for(varj=0; j<10;j++){4         if(i==5&&j==5){5             Continueoutermost;6         }7num++;8     }9  }TenAlert (num);

8.with statement : Used to set the scope of code to a specific object. Syntax: with (expression) statement;
  Note: the WITH statement is not allowed in strict mode. The use of with statements is not recommended for large-scale development applications because of the performance degradation caused by the large number of using with statements and the difficulty of debugging Code.

9.switch Statement.

  Note: (1) Any data type can be used in a switch statement.

(2) The switch statement uses the congruent operator when comparing values, so type conversions do not Occur.

JS Elevation 3. Basic Concept (5) 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.