1 – Operator (operator)

1.1 Classification of operators

Operators, also known as operators, are symbols used to perform functions such as assignment, comparison, and arithmetic operations.

Common operators in JavaScript are: - arithmetic operators - increment and decrement operators - comparison operators - logical operators - assignment operatorsCopy the code

1.2 Arithmetic operators

  • Overview of arithmetic operators

    Concept: symbol used in arithmetic operations to perform arithmetic operations on two variables or values.

  • The accuracy of floating-point numbers

    Floating-point values have a maximum accuracy of 17 decimal places, but are far less accurate in arithmetic than integers.

    var result = 0.1 + 0.2;// The result is 0.30000000000000004
    console.log(0.07 * 100);// The result is 7.000000000000001
    Copy the code

    So: Don’t directly determine whether two floating point numbers are equal!

  • Expressions and return values

    Expression: a combination of numbers, operators, variables, etc., in a meaningful arrangement that yields a numerical value

    Simple understanding: is composed of numbers, operators, variables, etc

    The expression will eventually have a result, called the return value, returned to the developer

1.3 Increment and decrement operators

  • Overview of increment and decrement operators

    If you need to repeatedly add or subtract 1 from a numeric variable, you can do this using the increment (++) and decrement (--) operators. In JavaScript, incrementing (++) and decrementing (--) can be placed either before or after variables. When placed before a variable, we can call it a pre-increment (decrement) operator, and when placed after a variable, we can call it a post-increment (decrement) operator. Note: Increment and decrement operators must be used with variables.Copy the code
  • Increment operator

    • Pre-increment operator

      ++num = num + 1

      Use the formula: first add, then return value

    var  num = 10;
    alert(++num + 10);   / / 21
    Copy the code
    • Postincrementing operator

      Num ++ is incremented by 1, similar to num = num+ 1

      Use the formula: first return the original value, then add

    var  num = 10;
    alert(10 + num++);  / / 20
    Copy the code

1.4 Comparison operators

  • Overview of comparison operators

    Concept: A comparison operator (relational operator) is an operator used when two data are compared and returns a Boolean value (true/false) as the result of the comparison operation.Copy the code

  • The equal sign is

console.log(18= ='18'); // true
console.log(18= = ='18'); // false
Copy the code

1.5 Logical operators

  • Overview of logical operators

    Boolean operators are operators that perform Boolean operations and return Boolean values. Multiple criteria are often used in later developmentCopy the code

  • Logic and &&

    Return true for both, false otherwise

  • Logic or | |

    Return true for both, false otherwise

  • The logical not!

    Logic is not (!) Also called negation, used to take a value that is the opposite of a Boolean, such as true where the opposite value is false

    var isOk = !true;
    console.log(isOk);  // false
    Copy the code
  • Short circuit operation (logical interrupt)

    When the expression value to the left of the parent of a logical element determines the result, the expression value to the right is not evaluated.

    • Logic and

      Syntax: expression 1 && expression 2

      - Returns expression 2 if the value of the first expression is trueCopy the code
      • If the value of the first expression is false, expression 1 is returned
      console.log( 123 && 456 );        / / 456
      console.log( 0 && 456 );          / / 0
      console.log( 123 && 456&& 789 );  / / 789
Copy the code
  • Logic or

    Expression expression syntax: 1 | | 2

    - Returns expression 1 if the value of the first expression is true - returns expression 2 if the value of the first expression is falseCopy the code
     console.log( 123 || 456 );         //  123
       console.log( 0 ||  456 );          //  456
       console.log( 123 || 456 || 789 );  //  123
    Copy the code

1.6 Assignment operators

Concept: Operator used to assign data to variables.

var age = 10;
age += 5;  // equivalent to age = age + 5;
age -= 5;  // equivalent to age = age-5;
age *= 10; // equivalent to age = age * 10;
Copy the code

1.7 Operator priority

  • The logic in unary operators is of high priority
  • Logic and higher than logic or priority

2 – Process control

2.1 Process control concept

In the process of a program execution, the execution sequence of each code has a direct impact on the results of the program. A lot of times we control the order in which code is executed to achieve what we want to do.

Simple understanding: Flow control is to control the code to execute in a certain structural order

There are three main structures of flow control: sequence structure, branch structure and cycle structure

2.2 Sequential process control

2.3 Branch flow control

  • Branching structure

JS language provides two kinds of branch structure statements: if statement, switch statement

  • If statement

    • Grammatical structure
    // If the condition is true, execute the code, otherwise do nothing
    if(conditional expression) {// The code statement to execute if the condition is true
    }
    Copy the code
    Statements can be understood as an action, and loop and branch statements are typical statements. A program consists of many statements, usually divided into separate statements.Copy the code
    • Execute the process

  • If else statement (double branch statement)

    • Grammatical structure

      If the condition is true, execute if; otherwise, execute else
      if(conditional expression) {// Execute the code if the condition is true
      } else {
          // [otherwise] executes the code
      }
      
      Copy the code
    • Execute the process

  • If else if statement (multi-branch statement)

    • Grammatical structure

      // It is suitable for checking multiple conditions.
      ifConditional expression1) {statement1; }else ifConditional expression2) {statement2; }else ifConditional expression3) {statement3; . }else {
          // Execute this code if none of the above conditions are true
      }
      
      Copy the code
    • Perform logical

2.4 Ternary Expressions

  • Grammatical structure

    expression1? expression2Expression:3;
    Copy the code
  • Implement ideas

    • If expression 1 is true, the value of expression 2 is returned; if expression 1 is false, the value of expression 3 is returned
    • Simple to understand: Similar to short for if else

2.5 Switch Branch Flow Control

  • Grammatical structure

    
    switch(expression){case value1:
          // The code to execute when the expression equals value1
          break;
      case value2:
          // The code to execute when the expression equals value2
          break;
      default:
          // The code to execute when the expression is not equal to any value
    }
    
    Copy the code
    • The keyword switch can be followed by an expression or value in parentheses, usually a variable

    • The keyword case, followed by the expression or value of an option, followed by a colon

    • The value of the switch expression is compared to the value of the case in the structure

    • If there is a matching congruence (===), the code block associated with the case is executed and stops when a break is encountered, terminating the entire switch statement code execution

    • If all case values do not match the expression values, execute the code in default

      Note: execute a statement in case without a break, proceed to execute the statement in the next case.

    Copy the code
  • Switch statements and if else if statements

    • In general, the two statements can be substituted for each other
    • switch… Case statements typically handle cases where case is determined for comparison, whereas if… The else… Statements are more flexible and are often used for range determination (greater than or equal to a range)
    • The switch statement is directly executed to the conditional statement of the program after the condition judgment, which is more efficient. And the if… Else statements have several conditions, so you have to judge them as many times as you want.
    • When there are fewer branches, if… Else statements execute more efficiently than switch statements.
    • Switch statements execute more efficiently and have a clearer structure when there are many branches.