Logical AND/logical OR

Precedence and associativity of logical operators

  • priority
    • The priority of logical AND is higher than logical OR
    • Example: { {true || false && false}}//truefirst calculate the logic and true || false
  • Associativity
    • Left associativity (operation from left to right)

In the logical AND and logical OR operators, if the data involved in the operation is not Boolean data, the return value has a characteristic

  • Logical and
    • If condition A is not established, return to condition A directly
      • Example: { {0 && 1}}//0/{ {null && 0}}//null
    • If condition A is established, regardless of whether condition B is established, return to condition B directly
      • Example: { {1 && 0}}//0/{ {1 && 2}}//2
  • Logical OR
    • If condition A is established, return to condition A directly
      • Example: { {1 || 0}}//1/{ {1 ||10}}//1
    • If condition A is not established, regardless of whether condition B is established, return to condition B directly
      • Example: { {0 || null}}//null/{ {0 || 996}}//996

Short-circuit phenomenon of logical AND and logical OR

  • Logical AND: condition A is not established, return condition A directly, condition B will not be executed
    • Examplelet num = 1;let res = (10 > 20) && (++num);console.log(res, num);//false,1
  • Logical OR: Condition A is established, condition A is returned directly, condition B will not be executed
    • Examplelet num = 1;let res = (10 < 20) || (++num);console.log(res, num);//true,1

Guess you like

Origin blog.csdn.net/big_sun_962464/article/details/112128448