JavaScript Advanced Programming Reading notes (vii) statement _javascript techniques in ECMAScript

Source: Internet
Author: User
If statement
Grammar:
Copy Code code as follows:

if (condition) {
Statement1;
}
else{
Statement2;
}

Iteration Statement
1, do-while statement
Grammar:
Copy Code code as follows:

do{
Statement
}while (expression);

2, while statement
Grammar:
Copy Code code as follows:

while (expression) {
Statement
}

3, for statement
Grammar:
Copy Code code as follows:

for (initialization;expression;post-loop-expression) {
Statement
}

4, for-in statement
Grammar:
Copy Code code as follows:

For (property in expression) {
Statement
}

Example:
Iterate through the array in four ways above:
Copy Code code as follows:

var iarr=new Array (1,2,3,4,5);
var index=0;

Do-while
do{
Console.log (Iarr[index]);
}while (++index<iarr.length);

While
index=0;
while (Index++<iarr.length) {
Console.log (Iarr[index-1]);
}

For
for (index=0;index<iarr.length;index++) {
Console.log (Iarr[index]);
}

For-in
for (x in Iarr) {
Console.log (Iarr[x]);
}

Tagged statements
You can label statements in the following syntax for later calls:

Label:statement
For example:

Start:var icount=10;
In this example, the label start can be invoked by later break statements or continue statements

Break statements and Continue statements

Both break and continue provide tighter control over the execution of code in the loop. The break statement can immediately exit the loop, and continue simply exits the current loop and goes to the next loop. Example:
Copy Code code as follows:

var inum=0;
for (Var i=1;i<10;i++) {
if (i%5==0) {
Break
}
inum++;
}
Console.log (inum);//4

inum=0;
for (Var i=1;i<10;i++) {
if (i%5==0) {
Continue
}
inum++;
}
Console.log (inum);//8

inum=0;
outer://Label
for (Var i=0;i<10;i++) {
for (Var j=0;j<10;j++) {
if (i==5&&j==5) {
Break outer;
}
inum++;
}
}
Console.log (inum);//55

inum=0;
outer://Label
for (Var i=0;i<10;i++) {
for (Var j=0;j<10;j++) {
if (i==5&&j==5) {
Continue outer;
}
inum++;
}
}
Console.log (inum);//95

With statement
The WITH statement is used to set the scope of the code in a particular object. Its syntax is as follows:
Copy Code code as follows:

With (expression) {
Statement
}

Usage examples:
Copy Code code as follows:

var smessage= "Hello World";
With (smessage) {
Console.log (toUpperCase ());//hello World
}

Switch statement
The sister statement of the IF statement is a switch statement. The switch syntax is as follows:
Copy Code code as follows:

switch (expression) {
Case value1:
Statement
Break
Case value2:
Statement
Break
...
Case Valuen:
Statement
Break
Default
Statement
}

The switch in ECMAScript can be used for strings, examples:
Copy Code code as follows:

var scolor= "Green";
Switch (scolor) {
Case "Red":
Console.log ("#FF0000");
Break
Case "Green":
Console.log ("#00FF00");//#00FF00
Break
Default
Console.log ("#FFFFFF");
}

Author: Tian Xing Jian, self-improvement
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.