javascript Operator A Comprehensive Analysis of Logical Operators

  • 2021-07-01 06:11:11
  • OfStack

Previous words

Logical operators perform Boolean operations on operands, and are often used in conjunction with relational operator 1. Logical operators combine multiple relational expressions to form a more complex expression. Logical operators are divided into logical non-'! ', logic and' & & ', logical or '', and this article will introduce these three logical operators

Logical non-

Logical non-operators consist of an exclamation point (! ) and can be applied to any value in ECMAScript. Regardless of the data type of this value, this operator will return 1 Boolean value. The logical non-operator first converts its operand to a Boolean value and then negates it

The conversion type of a logical non-aligned operand to a Boolean type is the same as the Boolean () conversion function, except that the result is finally inverted. If you use two logical non-operators at the same time, you will actually simulate the behavior of the Boolean () transition function


console.log(!!undefined);//false
console.log(!!null);//false
console.log(!!0);//false
console.log(!!-0);//false
console.log(!!NaN);//false
console.log(!!'');//false
console.log(!!false);//false

console.log(!!{});//true
console.log(!![]);//true

console.log(!!new Boolean(false));//true
console.log(!!false);//false
console.log(!!new Boolean(null));//true
console.log(!!null);//false

Logical non-operators are often used to control loops


//Boolean  Variable (bFound) Used to record whether the retrieval was successful. When the data item in the problem is found, bFound  Will be set to true , !bFound Will be equal to false Means that the run will jump out while Cycle 
var bFound = false;
var i = 0;
while (!bFound) {
 if (aValue[i] == vSearchValues) {
  bFound = true;
 } else {
  i++;
 }
}

Logical and

The logical and operator consists of two sum signs ( & & ) means that there are two operands, and the result returns true only if both operands are true, otherwise false


// Logical and (&&) Truth table of 
 No. 1 1 Operands      No. 1 2 Operands      Results 
true       true        true
true       false       false
false       true        false
false       false       alse

Logical and operations can be applied to any type of operand, not just Boolean values. If one of the operands is not a Boolean value, the logical AND operation does not return a Boolean value

Logical AND operations are short-circuited operations. If the first operand determines the result, the second operand will not be evaluated again

For logical AND, if the first operand is false, the result is false regardless of the value of the second operand, and the first operand is returned; If the first operand is true, the true or false of the result is the same as the true or false of the second operand, and the second operand is returned


// Except for false , undefined , null , +0 , -0 , NaN , '' This 7 False values, the rest are true values 
console.log('t' && ''); // Because 't' Is a true value, so return ''
console.log('t' && 'f'); // Because 't' Is a true value, so return 'f'
console.log('t' && 1 + 2); // Because 't' Is a true value, so return 3
console.log('' && 'f'); // Because '' Is a false value, so return ''
console.log('' && ''); // Because '' Is a false value, so return ''

var i = 1;
var result = (true && i++);
console.log(result,i);// Because true Is the true value, so execute the i++ , i Yes 2 , result Yes 1

var i = 1;
var result = (false && i++);
console.log(result,i);// Because false Is a false value, so it is not executed i++ , i Yes 1 , result Yes false

Logic and operators can be used together to return the value of the expression whose first Boolean value is false


console.log(true && 'foo' && '' && 4 && 'foo' && true);// ''

Relational operators take precedence over logical and ( & & ) and logical or () have high priority, so similar expressions can be written directly without adding parentheses


if(a+1==2 && b+2==3){
  //Todo  
}

You can use logical and operators instead of if structures


if (a == b) {
 doSomething();
}
//  Equivalent to 
(a == b) && doSomething();

Logic and operators are often used in callback functions


// If no parameter is given a Pass a value, then a Is the default undefined Is a false value, so it is not executed a() To prevent error reporting, if parameters are given a Pass a value, the function is executed a()
function fn(a){
  if(a){
    a();
  }
}
// Equivalent to 
function fn(a){
  a && a();
}

Logical or

The logical OR operator is represented by two vertical bars () and has two operands. The result returns false only if both operands are false, otherwise true


console.log(!!{});//true
console.log(!![]);//true

console.log(!!new Boolean(false));//true
console.log(!!false);//false
console.log(!!new Boolean(null));//true
console.log(!!null);//false
0

Likewise, logic or operations can be applied to any type of operand, not just Boolean values. If one of the operands is not a Boolean value, the logical OR operation does not return a Boolean value

Logical OR operations are also short-circuited operations. If the first operand determines the result, the second operand will not be evaluated

For logical OR, if the first operand is true, the result is true regardless of the value of the second operand, and the first operand is returned; If the first operand is fales, the true or false of the result is the same as the true or false of the second operand, and the second operand is returned


console.log(!!{});//true
console.log(!![]);//true

console.log(!!new Boolean(false));//true
console.log(!!false);//false
console.log(!!new Boolean(null));//true
console.log(!!null);//false
1

console.log(!!{});//true
console.log(!![]);//true

console.log(!!new Boolean(false));//true
console.log(!!false);//false
console.log(!!new Boolean(null));//true
console.log(!!null);//false
2

Likewise, logical or operators can be used together to return the value of an expression whose first Boolean value is true


console.log(false || 0 || '' || 4 || 'foo' || true);// 4

Logic or operators are often used to set default values for variables


console.log(!!{});//true
console.log(!![]);//true

console.log(!!new Boolean(false));//true
console.log(!!false);//false
console.log(!!new Boolean(null));//true
console.log(!!null);//false
4

Related articles: