This article mainly introduces the statements in JavaScript, including condition branch statements, loop statements, Iteration statements, Lable statements, break and continue statements, with statements, and swith statements, recommended to friends.
I. condition branch statement: If
Basic format:
If ( <表达式1> ){
<语句组1>
} Else if ( <表达式2> ){
<语句组2>
} Else {
<语句组3>
}
Execution process:
II. cyclic statements
2.1 Test loop statement before: Evaluate the exit condition before the code in the loop body is executed.
2.1.1while statement
Basic format:
Do {
<语句组>
} While ( <表达式> )
Execution process:
2.1.2 for statement
Basic format:
For ( <初始表达式> ; <条件表达式> ; <变动量表达式> ){
<语句组>
}
Execution process:
2.2 and then test the loop statement: exit conditions are tested only after the code in the loop body is executed.
2.2.1 do-while statement
Basic format:
Do {
<语句组>
} While ( <表达式> );
Execution process:
III. precise iterative statements: For-in
Basic format:
For (attribute in object ){
<语句组>
}
Purpose: repeat all attributes of a specified object and use them to enumerate attributes of an object.
Example:
The code is as follows:
Click the following Button to traverse the attributes of the object "person" cyclically.
Click here
Script
Function myFunction ()
{
Var x;
Var txt = "";
Var person = {fname: "Bill", lname: "Gates", age: 56 };
For (x in person)
{
Txt = txt + person [x];
}
Document. getElementById ("demo"). innerHTML = txt;
}
Script
If the object to be expressed is null or undefined, the loop body will no longer execute or throw an error. Therefore, when the for-in loop is executed, check whether the attribute value of the modified object is null or undefined.
IV,Lable statement
Basic syntax:
For example, begin: for (var I = 0; I <10; I ++ ){
Alert (I );
}
An example is provided to illustrate the functions of the lable statement:
Lable not added:
The code is as follows:
Var num = 0;
For (var I = 0; I <10; I ++ ){
For (var j = 0; j <10; j ++ ){
If (I = 5 & j = 5 ){
Break;
}
Num ++;
}
}
Alert (num); // 95
Add lable:
The code is as follows:
Var num = 0;
OutPoint:
For (var I = 0; I <10; I ++ ){
For (var j = 0; j <10; j ++ ){
If (I = 5 & j = 5 ){
Break outPoint;
}
Num ++;
}
}
Alert (num); // 55
In the first example, output 95 is not hard to understand. in the second example, output 55 is because when the break outpoint is executed, it jumps out to the putpoint layer and runs the alert statement.
If you change the second example to the following:
The code is as follows:
Var num = 0;
For (var I = 0; I <10; I ++ ){
OutPoint:
For (var j = 0; j <10; j ++ ){
If (I = 5 & j = 5 ){
Break outPoint;
}
Num ++;
}
};
Alert (num); // 95
The result is consistent with that in the first example.
V. break and continue statements
5.1break statement:
The code is as follows:
Var num = 0;
For (var I = 1; I <10; I ++ ){
If (I % 5 = 0 ){
Break;
}
Num ++;
};
Alert (num); // 4
After the break statement is executed, it jumps to the alert statement.
5.2continue statement:
The code is as follows:
Var num = 0;
For (var I = 1; I <10; I ++ ){
If (I % 5 = 0 ){
Continue;
}
Num ++;
};
Alert (num); // 8
After the continue statement is executed, it jumps to the for () loop and continues until the loop condition is invalid.
6. with statements
Basic syntax:
With (object ){
Statements
}
Example:
Do not use the with statement:
The code is as follows:
Var qs = location. search. substring (1 );
Var hostName = location. hostname;
Var url = location. href;
Alert (qs );
Alert (hostName );
Alert (url );
Use the with statement:
The code is as follows:
With (location ){
Var qs = search. substring (1 );
Var hostName = hostname;
Var url = href;
}
Alert (qs );
Alert (hostName );
Alert (url );
From the example above, we can see that the with statement is used to set the code scope to a specific object and reduce repeated input.
However, the interpreter of js needs to check whether the variables in the with block belong to the objects contained in with, which greatly reduces the execution speed of with statements and makes js statements difficult to be optimized.
Therefore, it is not recommended to use the with statement on a large scale.
VII. swith statements
Basic syntax:
Switch ( <表达式> ){
Case <数值1> : <语句组1>
Break;
Case <数值2> : <语句组2>
Break;
...
Default <语句组>
}
Execution process:
The switch statement can be any data type, and the value of each case is not necessarily a constant, or a variable or expression. for example:
The code is as follows:
Switch ("hello world "){
Case "hello" + "world ":
Alert ("Greeting was found .");
Break;
Case "goodbye ":
Alert ("Closing was found .");
Break;
Default:
Alert ("Unexpected message was found .");
}
The code is as follows:
Var num = 25;
Switch (true ){
Case num <0:
Alert ("Less than 0 .");
Break;
Case num> = 0 & num <= 10:
Alert ("Between 0 and 10 .");
Break;
Case num> 10 & num <= 20:
Alert ("Between 10 and 20 .");
Break;
Default:
Alert ("More than 20 .");
}
The switch statement uses the equal-sum operator for comparison, so no type conversion occurs.
Exercise:
The code is as follows: