logical AND and logical OR in js essay

Logical AND: &&, all are true

 

Logical OR: ||, all true is true

 

When both sides of the logical operation are not both boolean values, they will be implicitly converted to boolean values.
Conversion rules:
convert to true: non-zero number (including infinity), non-empty string
to false: 0, empty string, undefined, NaN , null


Short-circuit syntax
logic and a&&b: Similar to a series circuit, if a is true, it flows to b, and the value is b; if a is false, it does not flow to b, and the value is a;

 

Logical OR a || b: Similar to a parallel circuit, if a is true, the result is a; if a is false, through b, the result is b;

 

 

practise:

1  //Order of logical operations: NOT, AND, OR
 2  // Exercise 1: false || !false && false || true;
 3  var num1 = false || !false && false || true;
 4  /*
 5  false | | !false && false || true
 6  = false || true && false || true
 7  = false || false || true
 8  = false || true
 9  = true
 10  */
 11  //Non-boolean values ​​are participating in logical operations is implicitly converted to boolean
 12  // converted to true: non-zero number, non-empty string
 13  // converted to false: 0, empty string, undefned, null, NaN
 14  // exercise 2 4 && "hello " || !false || !true && null
 15  var num2 = 4 && "hello" || !false || !true && null;
16 /*
17  4 && "hello" || !false || !true && null
 18  = 4 && "hello" || true || false && null
 19  //Refer to the logic and series short circuit diagram, a && b, if a is true, The value is b; if a is false, the value is a; here 4 is implicitly converted to true, and the value is "hello",
 20  = "hello" || true || false    
 21  //Refer to the logic or parallel short circuit diagram, a || b, if a is true, the value is a; if a is false, the value is b
 22  = "hello" || false
 23  = "hello"
 24  */
 25  console.log(num1); //output true
 26 console.log(num2); //output hello

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325647131&siteId=291194637