JavaScript && and | | _javascript Techniques for alternative usage

Source: Internet
Author: User

Recently also have no mind to write articles, feel always have busy things, ah. But these days began to study titanium, found that its official production of the MVC Framework (Alloy) is quite good, just started to suffer from the lack of good code to learn, documents and less, so has not been to study in detail, and then found the original official Codestrong is a set of very good learning code, as long as the whole code to see through it, I believe that the use of alloy will be the basic ~

While looking at its source code, it is found that many places use the following usage:

$.clouds && ($.index.add ($.clouds));

The beginning is not very understanding, after all, the usual use of less, on the next Google to know that the original writing is very convenient and easy to use (in fact, the source of jquery is also a large number of uses this method). The following quotes a section of the Internet to find explanations && and | | Alternative usage in javascript:

A () && B (): If True after a () is executed, B () is executed and the value of B is returned, and if A () is returned false, the entire expression returns the value of a (), and B () is not executed;

A () | | B (): If True after a () is executed, the entire expression returns the value of a (), B () is not performed, and if A () is returned false, B () is executed and the value of B () is returned;

&& priority is higher than | |

After reading is quite clear, and then look at the specific code:

Alert ((1 && 3 | | 0) && 4); Results 4①
alert (1 && 3 | | 0 && 4);//Results 3 ②
alert (0 && 3 | | 1 && 4);//Results 4③ 

Analysis:
Statement ①:1&&3 return 3 => 3 | | 0 Return 3 => 3&&4 return 4
Statement ②: First executes 1&&3 return 3, returns 0 in execution 0&&4, finally executes the result comparison 3| | 0 Return 3
Statement ③: First executes 0&&3 return 0, returns 4 in execution 1&&4, finally executes the result comparison 0| | 4 return 4

Note: Integers other than 0 are true,undefined, NULL, and null string "" to false.

I feel that JavaScript is really very strong and flexible Oh, hehe ~ ~

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.