Boolean operator parsing

Source: Internet
Author: User

In a programming language, the importance of Boolean operators is comparable to equal operators. If you do not have the ability to test two value relationships, statements such as if...else and loops will be useless.

There are a total of 3 Boolean operators: Non-, with, or

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

The following describes a set of rules for the Boolean () transformation function.

Data type The value converted to true Value converted to False
Boolean True False
String Any non-empty string "" (empty string)
Number Any non-0 numeric value (including infinity) 0 and Nan
Object Any object Null
Undefined No Undefined


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

2. Logic and

The logic and operator are represented by two and number (&&), with two operands.

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

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

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

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



The code is as follows:


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

So let's take a look at the summary. && operations are mainly guided by several 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 CodeThe code is as follows:


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



3. Logical OR

The logic or symbol consists of two vertical bar symbols (| | Said
The logic or symbol is also a short-circuiting operator. The implementation process can refer to the logic and pseudo-code. No more rules are listed here.
The default handling of logic or often used to do parameters, such as Evt = EVT | | window.event;

Boolean operator parsing

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.