ECMAScript-262 defines a set of flow control statements that define the primary syntax in ECMAScript, which typically uses one or more keywords to accomplish a given task.
If statement
The syntax for the IF statement is as follows,
if (condition) statement1 else statement2
The condition (condition) can be an arbitrary expression, and the result of evaluating the expression is not necessarily a Boolean value. ECMAScript automatically calls the Boolean () conversion function to convert the result of this expression to a Boolean value. If the result of the condition job search is true, then statement1 (statement 1) is executed and Statement2 (statement 2) is executed if the result of condition evaluation is false.
1 {2 var i = +; 3 if (i >) {4 console.log (' Greater than '); 5 Else {6 console.log (' less than '); 7 }8 // output ' less than '9 }
If it's a continuous judgment, it can be written like this,
if (condition1) statement1 else if (condition2) Statement2 else Statement3
Do-while and while statements
The Do-while statement is a post-test loop statement that only the exit condition is tested after the code in the loop body is executed, and the code in the loop body is executed at least once before evaluating the conditional expression, while the while statement belongs to the pre-test loop statement, before the code in the loop body is executed. The export condition is evaluated, and the code in the loop body may never be executed. The following are the syntax of the two,
1 {2 // do-while Syntax 3 Do {4 statement5while (expression) 6 / while syntax 7while (expression) statement 8 }
For statement
The For statement is also a pre-test loop statement, but it has the ability to initialize variables and define the code to execute after the loop before executing the loop. Here is the syntax for the FOR statement,
for (initialization; expression; post-loop-expression) statement
1 {2 var count = ten; 3 var i; 4 for (i = 0; i < count; i++) {5 console.log (i); 6 }7 }
for-in statements
The for-in statement is a precise iterative statement that can be used to enumerate the properties of an object. The syntax is as follows,
For (property in experssion) statement
1 {2 for (var in window) {3 console.log (propname); 4 }5 }
Label statement
Use the Label statement to add tags to your code for future use. The syntax is as follows,
Label:statement
1 {2 var count = ten; 3 var i; 4 for (i = 0; i < count; i++) {5 console.log (i); 6 }7 }
Break and Continue statements
The break and continue statements are used to precisely control the execution of code in a loop. Where the break statement exits the loop immediately, forcing the statement following the loop to continue execution. While the continue statement exits execution immediately, it resumes from the top of the loop after exiting the loop.
1 {2 varnum = 0;3 for(vari = 1; I < 10; i++) {4 if(i% 5 = = 0) {5 Break;6 }7num++;8 }9Console.log (num)//4Ten } One { A varNUM1 = 0; - for(varj = 1; J < 10; J + +) { - if(i% 5 = = 0) { the Continue; - } -num1++; - } +Console.log (NUM1);//8 -}
Both the break and continue statements can be used in conjunction with a label statement to return a specific location in the code. This combination of usage occurs in cases where the loop is nested.
1 {2 varnum = 0;3 Outermost:4 for(vari = 0; J < 10; i++) {5 for(varj = 0; J < 10; J + +) {6 if(i = = 5 && J = = 5) {7 Breakoutermost;8 }9num++;Ten } One } AConsole.log (num);// - -}
With statement
The function of the WITH statement is to set the scope of the code to a specific object, and the purpose of defining the with statement is primarily to simplify the work of writing the same object multiple times, with the following syntax:
With (expression) statement;
1 {2 //var qs = location.search.substring (1);3 //var hostName = location.hostname;4 //var url = location.href;5 //The above code can be modified with the6 with(location) {7 varQS = search.substring (1);8 varHostName =hostname;9 varURL =href;Ten } One}
Switch statement
The switch statement is a flow control statement universally applicable in ECMAScript, with the following syntax,
1 {2 Switch(expression) {3 Casevalue:statement4 Break;5 Casevalue:statement6 Break;7 Casevalue:statement8 Break;9 default: StatementTen } One}
The meaning of each case in the switch statement is: "If the expression equals this value (value), then the following statement (statement) is executed." The break keyword causes the code execution flow to jump out of the switch statement. If you omit the break keyword, it causes the next case to continue after the current case is executed. The last default keyword is used to execute the code inside when the expression does not match any of the preceding cases.
Any data type can be used in the ECMAScript switch statement, whether it is a string or an object without problems. Second, the value of each case is not necessarily a constant, even an expression.
1 {2 Switch(i) {3 Case25:4Alert (' 25 ');5 Break;6 Case35:7Alert (' 35 ');8 Break;9 Case45:TenAlert (' 45 '); One Break; A default: -Alert (' Other '); - } the}
JavaScript Text-----Statements