Conditional Judgment Statement
1.if statement
if (condition) statement1 else statement2
The condition condition can be any expression, and the result of the evaluation of the expression is not necessarily a Boolean value. ECMAScript automatically invokes the Boolean () conversion function to convert the result of this expression to a Boolean value. If the result of the condition evaluation is true, the statement1 is executed, and if the condition evaluates to False, the statement2 is executed. The two statements can be either one line of code or a block of code (recommended).
if (condition1) statement1 else if (condition2) Statement2 else Statement3
2.switch statement
switch (expression) {case
value:statement break
;
Case Value:statement break
;
Case Value:statement break
;
Default:statement
}
The meaning of each case in a switch statement is: "If the expression equals this value, then the following statement statement." A break causes code execution to jump out of the switch statement. Omitting the break keyword causes the next case to continue after execution of the current case. The final default keyword is used to execute the maneuver code (and therefore the equivalent of an else statement) when the expression does not match any of the preceding situations.
Example 1
var num =;
Switch (true) {case
num < 0:
alert ("less than 0");
break;
Case num >= 0 && num <=:
alert ("Between 0");
break;
Case num > && num <=:
alert ("Between");
break;
Default:
alert ("More than");
}
NOTE: The switch statement uses the strict equality operator when comparing values, so no type conversions occur.
Loop statement
after the test loop statement.
Before evaluation of an expression, the code in the loop body is executed at least once.
1.do-while statement
Do {
statement
} while (expression);
Example 2
var i = 0;
Do {
i + 2;
} while (I <);
alert (i);//10
Pre-Test Loop statement
2.while Statement
while (expression) statement
Example 3
var i = 0;
while (I <) {
i = 2;
}
alert (i);//10
3.for statement
it has the ability to initialize variables and define loops before executing the loop.
Example 4
for (var i = 0;i < 10;i++) {
alert (i);
}
Iteration Statement
for-in Statement
The for-in statement is a precise iteration statement that can be used to enumerate the properties of an object
For (var prop in window) {
console.log (prop);
}
The properties of the ECMAScript object have no order. Therefore, the name of the property passed through the for-in loop output is unpredictable, in particular, all attributes are returned once, and the order of the SLR may vary depending on the browser. It is recommended that you check to make sure that the value of the object is not NULL or unedfined before you use it.
Controlling execution statements
Break statement
Exit Loop Now
Example 5
var num = 0;
for (var i = 1;i < 10;i++) {
if (i% 5 = 0) {break
;
}
num++;
}
alert (num);//4
Continue statement
exits the currently executing loop code, and then begins to continue the loop.
var num = 0;
for (var i = 1;i < 10;i++) {
if (i% 5 = 0) {
continue;
}
num++;
}
alert (num);//8
Return statement
you can specify a return value or do not specify (at this point, return undefined).
After the 1.return statement stops and exits the function immediately, the statement following the return will never execute
2.return statements can only appear in the body of a function, if not, it will automatically report syntax errors