Home > Web Front-end > JS Tutorial > body text

JavaScript Boolean operator parsing && || ! _javascript skills

WBOY
Release: 2016-05-16 17:50:56
Original
1106 people have browsed it
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
The logical AND operator is represented by two ampersands (&&) and has two operands. Logical AND operations can be applied to any type of operand, not just Boolean values. When one of the operands is not a Boolean value, the logical AND operation does not necessarily return a Boolean value; at this time, it follows the following rules: 1. If the first operand is an object, the second operand is returned. Two operands; 2. If the second operand is an object, the object will be returned only if the evaluation result of the first operand is true;
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.



Copy code The code is as follows: var nul = null;
var na = NaN;
var test;
test = na&&nul;
document.write(test); //NaN






Copy code


The code is as follows:
var nul = null; var na = NaN; var test; test = nul&&na; document.write(test); //null

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;
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template