JavaScript basic learning series twenty-five: conditional operators

The conditional operator is one of the most widely used operators in ECMAScript. The syntax is the same as in Java:

    variable = boolean_expression ? true_value : false_value;

The above code performs a conditional assignment operation, that is, determining which value to assign to the variable variable based on the value of the conditional expression boolean_expression. If boolean_expression is true, assign true_value; if boolean_expression is false, assign false_value. for example:

let max = (num1 > num2) ? num1 : num2;

In this example, max will be assigned a maximum value. The meaning of this expression is that if num1 is greater than num2 (the conditional expression is true), then num1 is assigned to max. Otherwise, assign num2 to max.

1. Assignment operator:

Simple assignment is represented by the equal sign (=), and the value on the right hand side is assigned to the variable on the left hand side, as follows: let num = 10;
compound assignment is represented by the multiplicative, additive, or bitwise operator followed by the equal sign (=) . These assignment operators are shorthand for common assignment operations like the following:

   let num = 10;
    num = num + 10;

The second line of code above can be accomplished with compound assignment:

   let num = 10;
    num += 10;

Every mathematical operator, as well as some other operators, has a corresponding compound assignment operator:

2. Comma operator:

The comma operator can be used to perform multiple operations in one statement, as shown below:

   let num1 = 1, num2 = 2, num3 = 3;

Declaring multiple variables in one statement is the most commonly used scenario for the comma operator. However, you can also use the comma operator to assist in assignment. Using the comma operator to separate values ​​during assignment will eventually return the last value in the expression:

let num = (5, 1, 4, 8, 0); // num的值为0

In this example, num will be assigned the value 0 because 0 is the last item in the expression. This use case of the comma operator is rare, but this behavior does exist.

ECMA-262 describes statements (also called flow control statements), and most of the syntax in ECMAScript is embodied in statements. Statements usually use one or more keywords to accomplish a given task. Statements can be simple or complex. It can be as simple as telling the function to exit, or as complex as listing a bunch of instructions to be executed repeatedly.

3. if statement:

The if statement is one of the most frequently used statements. The syntax is as follows:

if (condition) statement1 else statement2

The condition here can be any expression, and the evaluation result is not necessarily a Boolean value. ECMAScript will automatically call the Boolean() function to convert the value of this expression into a Boolean value. If the condition evaluates to true, statement1 is executed; if the condition evaluates to false, statement2 is executed.

You can use multiple if statements in succession like this:

if (condition1) statement1 else if (condition2) statement2 else statement3

Below is an example:

   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.");
}

4. 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 inside the loop is executed at least once. The syntax of do-while is as follows:

    do {
    
    
      statement
    } while (expression);

Below is an example:

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

In this example, the loop will repeat as long as i is less than 10. i starts from 0 and increments by 2 each time through the loop.

Guess you like

Origin blog.csdn.net/wanmeijuhao/article/details/135450420