Home > Article > Web Front-end > JavaScript study notes statements_basic knowledge
1. Conditional branch statement : if
Basic format:
if (
}else if (
}else{
}
Execution process:
2. Loop statement
2.1 Pre-test loop statements: The exit condition is evaluated 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 Post-test loop statement: The exit condition will be tested after the code in the loop body is executed.
2.2.1, do-while statement
Basic format:
do {
} while (
Execution process:
3. Accurate iteration statement : for-in
Basic format:
for (property in object){
}
Function: Repeatedly execute all properties of the specified object, which can be used to enumerate the properties of the object.
Example:
Click the button below to loop through the properties of the object "person".
If the object to be represented is null or undefined, the loop body will no longer be executed, or an error will be thrown. Therefore, when executing the for-in loop, you should first check whether the attribute value of the object is null or undefined.
4. Lable statement
Basic syntax:
Label:
For example: begin: for (var i = 0; i < 10 ; i ){
alert(i);
}
Give an example to illustrate the function of label statement:
Not labelable:
Join lable:
It is not difficult to understand that the first example outputs 95, but why the second example outputs 55 is because when the break outpoint is reached, it jumps directly to the putpoint layer and executes the alert statement.
If you change the second example to the following:
5. break and continue statements
5.1break statement:
6. with statement
Basic syntax:
with (object) {Statements
}
Example:
Writing without using with:
Use the writing method with:
But the js interpreter needs to check whether the variables in the with block belong to the object contained in with, which will greatly slow down the execution speed of the with statement and make the js statement difficult to optimize.
Therefore, it is not recommended to use the with statement on a large scale.
7. switch statement
Basic syntax:
switch (
case
break;
case
break;
...
default
}
The switch statement can be of any data type, and the value of each case is not necessarily a constant, but can also be a variable, expression, etc., for example:
The switch statement uses the equality operator for comparison, so no type conversion will occur.
Exercise:
for(;;){
alert("2");//How many times is 2 output?
}
The above is the entire content of this article, I hope it can be helpful to my friends.