Study Notes 5—JavaScript Statement

1 if statement

Syntax:
if (condition) statement1 else statement2
The condition here can be any expression, and the result of the evaluation is not necessarily a Boolean value. ECMAScript will automatically call the Boolean() function to convert the value of this expression to a Boolean value.

if(i>25){
    
    
	console.log("Greater than 25.");
}else if(i<0){
    
    
	console.log("Less than 0.");
}else{
    
    
	console.log("Between 0 and 25,inclusive.");
}

2 do-while statement

The do-while statement is a post-test loop statement, that is, the exit condition is evaluated after the code in the loop body is executed. In other words, the code in the loop is executed at least once:

do {
    
    
	statement
} while (expression);

Example:

        let i = 0;
        do {
    
    
            i += 2;
        } while (i < 10);

3 while statement

The while statement is a loop statement that first tests the exit condition, and then executes the code in the loop. The syntax is as follows:
while(expression) statement
example:

        let i = 0;
        while (i < 10) {
    
    
            i += 2;
        }

4 for statement

The syntax is as follows:
for (initialization;expression;post-loop-expression) statement
Example:

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

The above code defines the initial value of variable i to be 0 before the start of the loop. Then evaluate the conditional expression, and if the evaluation result is true (i<count), the loop body is executed. Therefore, the loop body may not be executed. If the body of the loop is executed, the expression after the loop will also be executed to increment the variable i.
Initialization, conditional expression and expression after loop are not necessary, so the following writing can create an infinite loop:

for (;;;){
    
    
	doSomething();
}
//如果只包含条件表达式,那么for循环实际上就变成了while循环
let count = 10;
let i = 0;
for (;i<count;){
    
    
	console.log(i);
	i++;
}

5 for-in statement

The for-in statement is a strict iterative statement that is used to enumerate non-symbolic key properties in an object. The syntax is:
for (property in expression) statement

6 for-of statement

The for-of statement is a strict iterative statement used to traverse the elements of an iterable object. The syntax is:
for (property of expression) statement

7 break and continue statements

The break and continue statements provide tighter control over the execution of loop code. The break statement is used to immediately exit the loop and force the execution of the next statement after the loop. The continue statement is also used to immediately exit the loop, but it will execute again from the top of the loop.

        let num = 0;

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

replace break with continue

        let num = 0;

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

8 switch statement

grammar:

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

Each case here is equivalent to: If the expression is equal to the following value, the following statement is executed. The break keyword will cause code execution to jump out of the switch statement. If there is no break, the code will continue to match the next condition. The default keyword is used to specify the statement to be executed by default when no conditions are met. With the switch statement, there is no need to write code similar to the following:

if (i == 25){
    
    
	console.log("25");
}else if(i == 35){
    
    
	console.log("35);
}else if(i == 45){
    
    
	console.log("45");
}else{
    
    
	console.log("Other");
}

Instead, you can write:

switch(i){
    
    
	case 25:
		console.log("25");
		break;
	case 35:
		console.log("35");
		break;
	case 45:
		console.log("45");
		break;
	default:
		console.log("Other");
}

The switch statement can be used for all data types, so you can use strings or even objects. Secondly, the value of the condition does not need to be a constant, but can also be a variable or an expression.

switch("hello world"){
    
    
	case "hello"+"world":
		console.log("Greeting was found");
		break;
	case "goodbye":
		console.log("Closing was found");
		break;
	default:
		console.log("Unexpected message was found");
}

9 functions

Basic syntax:

function functionName(arg0,arg1,...,argN){
    
    
	statements
}

Example:

function sayHi(name,message){
    
    
	console.log("Hello"+name+","+message);
}

You can call a function by its name, such as:
sayHi("Nicholas","how are you today"); The
output result is "Hello Nicholas, how are you today?"
The function in ECMAScript does not need to specify whether to return a value, only touch When the return statement is reached, the function will immediately stop executing and exit. The return statement can also have no return value. At this time, the function will immediately stop executing and return undefined.

Guess you like

Origin blog.csdn.net/qq_43599049/article/details/112974125