1. Logical negation Logical negation is not used! Indicates that it can be applied to any type of value in ECMAScript. The logical NOT operation returns a Boolean value (true/false). This operator first converts its operand to a Boolean value and then negates it.
The following explains a set of rules for the Boolean() transformation function.
数据类型 |
转换为true的值 |
转换为false的值 |
Boolean |
true |
false |
String |
任何非空字符串 |
""(空字符串) |
Number |
任何非零数字值(包括无穷大) |
0和NaN |
Object |
任何对象 |
null |
Undefined |
无 |
undefined |
Data type
|
The value converted to true
|
The value converted to false |
Boolean |
true |
false
|
String |
Any non-empty string |
""(empty string)
|
Number |
Any non-zero numeric value (including infinity) |
0 and NaN
|
Object |
Any object |
null
|
Undefined |
None |
undefined
|
A simple representation of the Boolean() transformation function. ----------! !
2. Logical AND
3. If two If the operators are all objects, then the second operand is returned; follow the first rule.
4. If one operation is null, return null;
5. If one operator is NaN, return NaN;
6. If one operator is undefined, return undefined.
The logical AND operation is a short-circuit operation, that is, if the first operand can determine the result, then the second operand will not be evaluated. (Can be understood as two internal return operations). Therefore, when rules 4, 5, and 6 conflict, the short-circuit operation principle shall be followed.
var na = NaN;
var test;
test = na&&nul;
document.write(test); //NaN
Copy code
The code is as follows:
So, let’s summarize. The && operation mainly follows several principles:
1. Short-circuit operation principle;
2. During the evaluation process, it will be transformed to generate a copy, but the return value is the original value;
Copy code
The code is as follows: // Pseudo code
function &&(param1,param2){
bparam1 = Boolean(param1); if (!bparam1) return param1; bparam2 = Boolean(param2); return param2; } // Return when the parameter is an object The referenced pointer value, the function is the same as 3. Logical OR The logical OR symbol is represented by two vertical bar symbols (||). The logical OR symbol is also a short-circuit operator. The implementation process can be referred to the pseudo code of logical AND. Some rules are not listed here. Logical OR is often used for default processing of parameters, such as evt = evt || window.event;
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn