SoFunction
Updated on 2025-03-10

JavaScript operator—Comprehensive parsing of logical operators

The previous words

Logical operators perform boolean operations on operands and are often used in conjunction with relational operators. Logical operators combine multiple relational expressions into a more complex expression. Logical operators are divided into three types: logical non-'!', logical and '&&', logical or '||'. This article will introduce these three logical operators.

Logical non-logical

The logical non-operator is represented by an exclamation mark (!) and can be applied to any value in ECMAScript. Regardless of the data type this value is, this operator returns a Boolean value. The logical non-operator first converts its operand into a boolean value and then inverses it.

The conversion type of logical non-pair operands to Boolean types is the same as the Boolean() transformation function, except that the result is inverted in the end. If two logical non-operators are used at the same time, the behavior of the Boolean() transformation function will actually be simulated.

(!!undefined);//false
(!!null);//false
(!!0);//false
(!!-0);//false
(!!NaN);//false
(!!'');//false
(!!false);//false
(!!{});//true
(!![]);//true

(!!new Boolean(false));//true
(!!false);//false
(!!new Boolean(null));//true
(!!null);//false

Logical non-operators are often used to control loops

//Boolean variable (bFound) is used to record whether the search is successful.  When the data item in the question is found, bFound will be set to true, !bFound will equal false, meaning that the run will jump out of the while loopvar bFound = false;
var i = 0;
while (!bFound) {
 if (aValue[i] == vSearchValues) {
  bFound = true;
 } else {
  i++;
 }
}

Logic and

The logic and operator is represented by two sums (&&). There are two operands. The result will return true only if both operands are true, otherwise false will be returned.

//Logistic and (&&) truth tableThe first operand    The second operand    result
true       true        true
true       false       false
false       true        false
false       false       alse

Logic and operations can be applied to any type of operand, not just booleans. If one of the operands is not a Boolean, then logic and operations do not necessarily return a Boolean value

Logic and operations are short-circuit operations. If the first operand can determine the result, then the second operand will no longer be evaluated.

For logic, if the first operand is false, no matter what value the second operand is, the result is false, and if the first operand is true, the result is returned; if the first operand is true, the true and false of the result are the same as the true and false of the second operand, the second operand is returned.

// Except for the 7 false values, undefined, null, +0, -0, NaN, and '', the rest are all true values('t' && ''); //Because 't' is the true value, return ''('t' && 'f'); //Because 't' is the true value, return 'f'('t' && 1 + 2); //Because 't' is the true value, return 3('' && 'f'); //Because '' is a false value, so return ''('' && ''); //because''It's a false value, so return''
var i = 1;
var result = (true && i++);
(result,i);//Because true is the true value, execute i++, i is 2, result is 1
var i = 1;
var result = (false && i++);
(result,i);//becausefalseIt's a false value,So don't executei++,iyes1,resultyesfalse

Logic and operators can be used in multiple ways, returning the value of the first Boolean expression with false

(true && 'foo' && '' && 4 && 'foo' && true);// ''

The priority of relational operators is higher than that of logic and (&&) and logic or (||), so similar expressions can be written directly without adding parentheses.

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

Logic and operators can be used instead of if structure

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

Logic and operators are often used in callback functions

//If the value is not passed to parameter a, a is the default undefined and is a false value, so a() is not executed to prevent errors. If the value is passed to parameter a, the function a() is executedfunction fn(a){
  if(a){
    a();
  }
}
//Equivalent tofunction fn(a){
  a && a();
}

Logical or

The logical or operator is represented by two vertical lines (||). There are two operands. Only when both operands are false will the result return false, otherwise true will be returned.

//Logical or (||) truth tableThe first operand    The second operand    result
true       true       true
true       false       true
false       true       true
false       false       false

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

Logic or operation is also a short-circuit operation. If the first operand can determine the result, then the second operand will no longer be evaluated.

For logic 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 false, the true and false of the result are the same as the true and false of the second operand, the second operand is returned.

('t' || '');//Because 't' is the true value, return "t"('t' || 'f');//Because 't' is the true value, return "t"('' || 'f');//Because '' is a false value, return "f"('' || '');//because''It's a false value,So return""
var i = 1;
var result = (true || i++);
(result,i);//Because true is the true value, i++ is not executed, result is true, i is 1
var i = 1;
var result = (false || i++);
(result,i);//becausefalseIt's a false value,So executei++,iyes2,resultyes1

Similarly, logic or operators can be used in multiple concatenation to return the value of the first Boolean expression with true value.

(false || 0 || '' || 4 || 'foo' || true);// 4

Logical or operators are often used to set default values ​​for variables

//If no object is passed to parameter p, the parameter is set to an empty object by defaultfunction fn(p){
  p = p || {};
}

The above is the full analysis of the javascript operator - logical operators brought to you by the editor. I hope everyone supports me~