Logical and & & and logical or

Logic and

tip1: logic and operation can be applied to any type of operand. When an operand is not Boolean, it does not necessarily return a Boolean value. The general rules are as follows:

1. If the first operand is an object, the second operand is returned;

2. When the second operand is an object, the object is returned only when the evaluation result of the first operand is true

3. If both operands are objects, the second operand is returned;

4. When the first operand is null, null is returned;

5. When the first operand is NaN, NaN is returned;

6. If the first operand is undefined, undefined is returned;

tip: if the evaluation result of the first operand is false, the subsequent operands will not judge execution. No matter what the value of the second operand is, the final result will not be true

tip: returns the second operand (in most cases)

See the code, for example:

 var obj={
            "name":"zhan",
            "age":25
        }
        var obj1={
            "name":"hui",
            "age":25
        }
        var boolean_0=false;
        var boolean_1=true;
        var null_=null;
        var isNan=NaN;
        var str='zhan'
        var undefined_=undefined;
        var num_0=0;
        var num_1=1;
        console.log(obj&&boolean_1)//true
        console.log(obj&&boolean_0)//false
        console.log(obj&&null_)//null
        console.log(obj&&isNan)//NaN
        console.log(obj&&str)//'zhan'
        console.log(obj&&undefined_)//undefined
        console.log(obj&&obj1)//obj1
        console.log(obj&&num_0)//0
        console.log(obj&&num_1)//1

        console.log(null_&&num_1)//null
        console.log(isNan&&num_1)//NaN
        console.log(undefined_&&num_1)//undefined

Logic or

tip: returns the first operand (in most cases) 1. If the first operand is an object, the first operand is returned;

2. If the evaluation result of the first operand is false, the second operand will be returned;

3. If both are objects, the first operand is returned;

4. If both operands are null, null is returned;

5. If both operands are NaN, NaN is returned;

6. If both operands are undefined, undefined is returned;

For example:

        console.log(obj||boolean_1)//obj
        console.log(obj||boolean_0)//obj
        console.log(obj||null_)//obj
        console.log(obj||isNan)//obj
        console.log(obj||str)//obj
        console.log(obj||undefined_)//obj
        console.log(obj||obj1)//obj
        console.log(obj||num_0)//obj
        console.log(obj||num_1)//obj

        console.log(null_||null_)//null
        console.log(isNan||isNan)//NaN
        console.log(undefined_||undefined_)//undefined

Posted by dfego on Fri, 03 Apr 2020 02:29:22 -0700