current position:Home>06. JavaScript statement

06. JavaScript statement

2022-04-29 12:42:25Hold the [email protected]

The journey of life , You can't use your eyes to capture every bit of life in other people's stories . With your own hands , Create your own dreams .
I am a vegetable chicken front-end lover , It's been a few months since I was on the front road , Finally, I decided to take a look at the front-end classic books , Here are my notes , I hope you can correct me .

1、 brief introduction

ECMA-262  Describes some statements ( Also known as flow control statements ), and  ECMAScript  Most of the grammar in is embodied in sentences .
 Statements usually use one or more keywords to accomplish a given task . Statements can be simple , It can also be complicated .

2、if sentence

【 grammar 】:

if (condition) statement1 else statement2

【 Syntax parsing 】:

  • there condition It can be any expression , And the result is not necessarily Boolean .ECMAScript Automatically called Boolean() Function to convert the value of this expression to a Boolean value .
  • If the condition evaluates to true, Then execute the statement statement1
  • If the condition evaluates to false, Then execute the statement statement2

【 Single if…else Case presentation 】:

if (i > 25) {
    
	console.log(" Greater than 25");
}else{
    
	console.log(" Less than or equal to 25");
}

【 Multiple if Case presentation 】:

if (i > 25) {
    
	console.log(" Greater than 25");
} else if (i < 0) {
    
	console.log(" Less than 0");
} else {
    
 console.log("0-25");
} 

3、do-while sentence

do-while  Statement is a post test loop statement , That is, the exit condition will not be evaluated until the code in the loop body is executed 

【 grammar 】:

do {
    
 statement
} while (expression);

【 Case presentation 】:

let i = 0;
do {
    
 i += 2;
} while (i < 10); 
//  as long as  i  Less than  10, The loop will repeat .i  from  0  Start , Increment each cycle  2.

4、while sentence

while  A statement is a statement that tests a loop first , That is, first detect the exit condition , Then execute the code inside the loop . therefore ,while  The code in the loop body may not execute 

【 grammar 】:

while(expression) statement

【 Case presentation 】:

let i = 0;
while (i < 10) {
    
 i += 2;
}
//  In this case , Variable  i  from  0  Start , Increment each cycle  2. as long as  i  Less than  10, The cycle will continue .

5、for sentence

for  Statement is also a test statement first , It just adds the initialization code before entering the loop , And the expression to be executed after the loop execution 	

【 grammar 】:

for (initialization; expression; post-loop-expression) statement

【 Case presentation 】:

let count = 10;
for (let i = 0; i < count; i++) {
    
 console.log(i);
} 

The clearest way to write is to use let Declare iterator variables , In this way, the scope of this variable can be limited to the loop

6、for-in sentence

 Is a strict iterative statement , Used to enumerate non symbolic key properties in an object 

【 grammar 】:

for (property in expression) statement 

【 Case presentation 】:

for (const propName in window) {
    
 document.write(propName);
} 

【 matters needing attention 】:

  • for-in Statement does not guarantee the order in which object properties are returned
  • If for-in The variables to iterate over the loop are null or undefined, The loop body... Is not executed .

7、for-of sentence

for-of  Statement is a strictly iterative statement , Used to iterate over the elements of an iteratable object 

【 grammar 】:

for (property of expression) statement

【 Case presentation 】:

for (const el of [2,4,6,8]) {
    
 document.write(el);
} 

8、break and continue sentence

break  and  continue  Statement provides more strict control over the execution of loop code .

【 Similarities and differences 】:

  • break Statement to exit the loop immediately , After the next loop of the statement
  • continue Statement is also used to exit the loop immediately , But it starts again at the top of the loop

【 Case presentation 】:

let num = 0;
for (let i = 1; i < 10; i++) {
    
 if (i % 5 == 0) {
    
 break;
 }
 num++;
}
console.log(num); // 4
let num = 0;
for (let i = 1; i < 10; i++) {
    
 if (i % 5 == 0) {
    
 continue;
 }
 num++;
}
console.log(num); // 8

9、switch sentence

【 grammar 】:

switch (expression) {
    
 case value1:
 statement
 break;
 case value2:
 statement
 break;
 case value3:
 statement
 break;
 case value4:
 statement
 break;
 default:
 statement
} 

【 Case presentation 】:

if (i == 25) {
    
 console.log("25");
} else if (i == 35) {
    
 console.log("35");
} else if (i == 45) {
    
 console.log("45");
} else {
    
 console.log("Other");
}
//  It can be written like this :
switch (i) {
    
 case 25:
 console.log("25");
 break;
 case 35:
 console.log("35");
 break;
 case 45:
 console.log("45");
 break;
 default:
 console.log("Other");
} 

【 matters needing attention 】:

  • To avoid unnecessary conditional judgment , It's better to add... To every condition break sentence

copyright notice
author[Hold the [email protected]],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204291113110033.html

Random recommended