Javascript Boolean operator Summary

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. In the weak type language like JavaScript more magical, let our code, simple and reliable. There are 3 Boolean operators: And, or, non.

Characteristics:

Non-

Let's talk first, the simplest of ' No! ’

Relative to and or to, not simpler, the operand is converted to a Boolean value, then reversed, the value is not true, is false.

Conclusion: Logical non-return values last only two: TRUE or false.

With AND OR:

But with and or not always return Boolean values,&& and | | The operation follows several major principles:

1. Short-circuit Operation principle: If the first operand determines the result of the operation, the second operand is no longer evaluated and the first action value is returned directly.

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

&& in logical operations in JavaScript | | The operation does not always return True and False, and the value it returns is the value of the last determined result , for example:

null&&true         //NULL to determine the result returns nulltrue&&undefined//undefined determine the result return undefinedObject&&{}&&[]&&2//returns 2Object&&{}&&2&&[]//Empty ArrayNull|| 9//Value of 9NULL|| 9| | Undefined//=9NULL|| Undefined//undefined

In summary, say:

&& operation: When the value is true, the last value is returned to the most to determine the result ;

When the value is False, the first false value is returned, and the result of the false value is determined to be false .

|| Action : When the value is true, returns the first truth, once the true result is true ;

When the value is false, the most value is returned to the most to determine the result .

so say, The value of the final determination of the result, as well as or Operation . .

Conclusion: The value of the final determination of the result, as well as or Operation .

Usage:

You can write a concise code.

And:

1. Assign a value by judging whether an object exists or not ;

// the value of Object2 is assigned to object when Object1 exists.

2. Can prevent access to the property error (the same principle 1);

Querying a non-existent property does not give an error, but if the object does not exist, try querying the nonexistent attribute for an error.

var box;var len=box.length; // throws a type error exception, the program does not parse down var len=box&&box.length; // no error .

Or:

The value can be selected first

        // Priority Object1

Javascript Boolean operator Summary

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.