1. Non-logical
The logic is not used! It can apply any type of value with ECMAScript. A boolean value (true/false) is returned for a logical non-operation ). This operator first converts its operands to a Boolean value and then reverse it.
The following describes a set of rules for the Boolean () transformation function.
Data Type |
Convert to true |
Convert to false |
Boolean |
True |
False |
String |
Any non-empty string |
"" (Null String) |
Number |
Any non-zero numeric value (including infinity) |
0 and NaN |
Object |
Any object |
Null |
Undefined |
None |
Undefined |
A simple representation of a Boolean () transformation function. -----------!!
2. Logic and
The logic and operator are represented by two ampersand (&) and have two operands.
Logic and operations can be applied to any type of operands, not just boolean values. When an operand is not a Boolean value, the logic and operation do not necessarily return a Boolean value. In this case, it follows the rules below:
1. If the first operand is an object, the second operand is returned;
2. If the second operand is an object, this object is returned only when the evaluate result of the first operand is true;
3. If both operators are objects, the second operand is returned. The first rule is followed.
4. If one operation is null, null is returned;
5. If one operator is NaN, NaN is returned;
6. If an operator is undefined, undefined is returned.
The logic and operation are short-circuit operations. That is, if the first operand determines the result, the second operand will not be evaluated. (It can be understood as two internal return operations ). Therefore, when rules 4, 5, and 6 conflict, the short-circuit operation principle is followed.
Copy codeThe Code is as follows:
Var nul = null;
Var na = NaN;
Var test;
Test = na & nul;
Document. write (test); // NaN
Copy codeThe Code is as follows:
Var nul = null;
Var na = NaN;
Var test;
Test = nul & na;
Document. write (test); // null
So let's make a summary. & The operation mainly follows the following principles:
1. Short Circuit operation principle;
2. A copy is generated during the evaluation process, but the returned value is the original value;
Copy codeThe Code is as follows:
// Pseudocode
Function & (param1, param2 ){
Bparam1 = Boolean (param1 );
If (! Bparam1) return param1;
Bparam2 = Boolean (param2 );
Return param2;
}
// When a parameter is an object, the referenced pointer value is returned. The function is the same
3. Logic or
The logic or symbol is represented by two vertical bars (|.
The logic or symbol is also a short-circuit operator. The implementation process can refer to the logic and pseudo code. Some rules are not listed here.
Logical or often used for parameter default processing, such as evt = evt | window. event;