JavaScript boolean operator parsing && | | ! _javascript Tips

Source: Internet
Author: User
1. Logical Non-

Logic is not used! Indicates that a value of any type can be applied to the ECMAScript, and the logical non operation returns a Boolean value (True/false). The operator first converts its operand to a Boolean value, and then it is reversed.

A set of rules for the next Boolean () transformation function is described below.
Data type Value converted to True The value converted to false
Boolean True False
String Any non-empty string "" (empty string)
Number Any value other than 0 digits (including infinity) 0 and Nan
Object Any object Null
Undefined No Undefined

A simple representation method of Boolean () transformation function. -----------!!

2, Logic and

The logic and operators are represented by two numbers (&&) and have two operands.

Logic and operations can be applied to any type of operand, not just a Boolean value. In the case where an operand is not a Boolean, the logic and operation do not necessarily return a Boolean value; At this point, it follows the rules:

1. Returns the second operand if the first operand is an object;
2. If the second operand is an object, the object is returned only if the first operand evaluates to true;
3. If the two operators are objects, return the second operand, followed by the first rule.
4. Returns null if one of the operations is null;
5. If one operator is Nan, return nan;
6. If an operator is undefined, return undefined.

Logic and operation are short-circuit operations, that is, if the first operand can determine the result, then the second operand will not be evaluated. (Can be understood as the internal two return operations). Therefore, when the 4, 5, 6 rules conflict, follow the short-circuit operation principle.

Copy Code code as follows:

var nul = null;
var na = NaN;
var test;
Test = na&&nul;
document.write (test); NaN


Copy Code code as follows:

var nul = null;
var na = NaN;
var test;
Test = nul&&na;
document.write (test); Null


So, let's summarize. && operation mainly follows several major principles:

1. Short circuit operation principle;
2. The evaluation process will be transformed to generate a copy, but the return value is the original value;

Copy Code code as follows:

Pseudo code
function && (param1,param2) {
bparam1 = Boolean (param1);
if (!bparam1) return param1;
Bparam2 = Boolean (param2);
return param2;
}
parameter is a pointer value that returns a reference when an object is


3, logic or

The logic or symbol consists of two vertical bar symbols (| | Said
Logic or symbols are also short circuiting operators. The implementation process can refer to the logic and pseudo code. There are no more rules listed here.
Logic or often used to do default processing of parameters, such as Evt = EVT | | window.event;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.