Previously, logical operators perform Boolean operations on operands, which are often used in the same way as Relational operators. Logical operators combine multiple relational expressions to form a more complex expression. Logical operators are classified into non-logical operators! , Logic and & amp;, logic, or | this article introduces these three logical operators logical non-operators by an exclamation point (!) It can be applied to ECMAScript.
Previous
Logical operators perform Boolean operations on operands, which are often used in the same way as Relational operators. Logical operators combine multiple relational expressions to form a more complex expression. Logical operators are divided into logical non '! ', Logical and' & ', logical or' | '. This article introduces these three logical operators.
Non-logical
The logical non-operator is composed of an exclamation mark (!) It can be applied to any value in ECMAScript. Regardless of the Data Type of the value, this operator returns a Boolean value. Logical non-operators first convert their operands into a Boolean value and then reverse it.
The conversion type of a non-pair operand to a Boolean type is the same as that of a Boolean () transformation function, except that the result is reversed. If two logical non-operators are used at the same time, the behavior of the Boolean () transformation function will be simulated.
console.log(!!undefined);//falseconsole.log(!!null);//falseconsole.log(!!0);//falseconsole.log(!!-0);//falseconsole.log(!!NaN);//falseconsole.log(!!'');//falseconsole.log(!!false);//false
console.log(!!{});//trueconsole.log(!![]);//trueconsole.log(!!new Boolean(false));//trueconsole.log(!!false);//falseconsole.log(!!new Boolean(null));//trueconsole.log(!!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 question is found, bFound will be set to true ,! BFound is equal to false, meaning that the running will jump out of the while LOOP var bFound = false; var I = 0; while (! BFound) {if (aValue [I] = vSearchValues) {bFound = true ;}else {I ++ ;}}
Logic and
The logic and operator are represented by two ampersand (&). There are two operands. true is returned only when both operands are true. Otherwise, false is returned.
// Logical and (&) truth table first operand second operand result true truetrue false falsefalse true falsefalse false alse
Logic and operations can be applied to any type of operands, not just boolean values. If one of the operands is not a Boolean value, the logic and operation do not necessarily return a Boolean value.
The logic and operation are short-circuit operations. If the first operand determines the result, it will not evaluate the second operand.
For logic and, if the first operand is false, the first operand is returned regardless of the value of the second operand and the result is false. If the first operand is true, if the result is true or false and the second operand is true or false, the second operand is returned.
// Except for the false, undefined, null, + 0,-0, NaN, and ''values, the other values are true value consoles. log ('T' & ''); // returns ''console because 't' is the true value. log ('T' & 'F'); // because 't' is the true value, the 'F' console is returned. log ('T' & 1 + 2); // returns 3console because 't' is the true value. log (''& 'F'); // returns ''console because'' is a false value. log (''&''); // returns ''because'' is a false value''
Var I = 1; var result = (true & I ++); console. log (result, I); // because true is the true value, execute I ++, I is 2, and result is 1var I = 1; var result = (false & I ++); console. log (result, I); // because false is a false value, I ++ is not executed, I is 1, and result is false.
The logic and operator can be used together to return the value of the first expression with a Boolean value of false.
console.log(true && 'foo' && '' && 4 && 'foo' && true);// ''
The priority of Relational operators is higher than that of logic and (&) and logic or (|). Therefore, similar expressions can be written directly without parentheses.
if(a+1==2 && b+2==3){ //Todo }
Logic and operators can be used to replace the if structure.
If (a = B) {doSomething () ;}// equivalent to (a = B) & doSomething ();
Logic and operators are often used in callback functions.
// If a is not passed to parameter a, a is the default undefined, which is a false value. Therefore, a () is not executed to prevent errors. If a value is passed to parameter, then the execution function a () function fn (a) {if (a) {a () ;}// equivalent to function fn () {a & ();}
Logic or
The logic or operator is represented by two vertical bars (|) and has two operands. The result returns false only when both operands are false. Otherwise, the return value is true.
// Logical or (|) truth table first operand second operand result true truetrue false truefalse true truefalse false
Likewise, logic or operations can be applied to any type of operands, not just boolean values. If one of the operands is not a Boolean value, the logic or operation does not necessarily return a Boolean value.
The logic or operation is also a short-circuit operation. If the first operand determines the result, it will not evaluate the second operand.
Logically or, if the first operand is true, the first operand is returned regardless of the value of the second operand and the result is true. If the first operand is fales, if the result is true or false and the second operand is true or false, the second operand is returned.
Console. log ('T' | ''); // returns the" t "console because 't' is the true value. log ('T' | 'F'); // returns the "t" console because 't' is the true value. log (''| 'F'); // returns the" f "console because'' is a false value. log (''|''); // returns "" Because ''is a false value ""
Var I = 1; var result = (true | I ++); console. log (result, I); // because true is the true value, I ++ is not executed, result is true, and I is 1var I = 1; var result = (false | I ++); console. log (result, I); // because false is a false value, I ++ is executed, I is 2, and result is 1.
Likewise, logic or operators can be used together to return the value of the first expression with a Boolean value of true.
console.log(false || 0 || '' || 4 || 'foo' || true);// 4
Logical or operators are often used to set default values for variables.
// If no object is input to parameter p, the default value of this parameter is function fn (p) {p = p | {};}
The above is a comprehensive understanding of the logic operators of javascript operators. For more information, see other related articles in the first PHP community!