JavaScript && and | | The alternative use technique of operation _javascript skill

Source: Internet
Author: User
&& and | | In the jquery source code is particularly widely used, because I do not have the system to learn JS, so only a cursory understanding of the self, I hope we can guide.
A rough understanding is as follows:

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 | |

As follows:
  
Code
Copy Code code as follows:

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.

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.