Deep understanding of the usage and/or non-usage of logical expressions-basic knowledge

Source: Internet
Author: User

First, logic and (&&), it can be understood from three levels

The simplest of the first level is the logic between simple Boolean values, where both the left and right values are true, return true when both sides are false or the values on either side are Fasle and return false;

The second level, (False,null,indefined,0,-0,nan and "" These are false values, all other values including the object are true), to these "truth" and "false value" and operation, return a "truth" or "false value";

It is noteworthy that (&&) does not return has been a Boolean value, he will return the second level appears in the truth or false value, but these "truth" and "false value" in the end is what value? So introducing the third level of understanding

The third level of understanding, in fact, is also simple, when the left operand in a statement is a false value, the right-hand operand of the statement is not evaluated, and the left-hand operand is returned directly as the result of the operation of the expression; when the left-hand operand is true, the value of the right-hand operand is returned as the result of the expression's operation,

Here are some examples:

var o={s:1};       Create an object 
var p=null;         Created a null 
O&&O.S            //Return of 1, should be O is true, so return O.s value 
p&&p.x            //return null, should be p is false value, So do not compute p.x, directly return the value of P <BR>o&&o.b;           This throws a type error exception because O is true and needs to return to O.B, but O.B does not exist. 

That's a good idea.

Although && can be used to return truth and false values, in most cases the truth value is true and false values are false;

Again, logic or (| |), extrapolate, logic, and also with logical or similar three levels of understanding, are not written in detail

The logical and most commonly used method is to select a true value expression from a group of alternative operands

As an example

var min =min_value| | sum.min_value| | 100;

Find Min_value First, if it is not defined, find it in the Sum object, and if not, assign it a dead value of 100,

This method is usually used to set the default value for a parameter in a function

As an example

function Add (a,b) {
   b=b| | 0;
   return a+b;  
}

When the initial value is not set to B, B equals the 0;add function returns the A+0, which is the value of A;

Logical NON (!)

His aim is to reverse the Boolean value of the operand, for example: If X is true,!X returns false, and!x returns real if X is a false value.

When you return a Boolean value, you have converted these truth values, false value, to a Boolean value, and then you reverse it.

Here to introduce a! Common usage of

Sometimes we need to check whether a variable exists or if the value has a valid value, use the!!,

For example, to detect whether a value returned from a function student is a valid value, use!! Student, if he returns true, then he is a valid value.

The above in-depth understanding of the use of logical expressions and or not the use of small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.