JS && | | Trap JavaScript logic with, logical, or "go"

Source: Internet
Author: User
Tags arithmetic

In general, logical operations A&&b and a| | B is the logic and operation and the logic or operation, the return is a Boolean value, either True or false. For example, in PHP, the a&&b return type is always a Boolean value, not true or false. and JavaScript in the && | | is a trap, JS inside the && and | | A little out of the ordinary way. Summary js && | | Rule: The following A and B can be a value, a variable, an expression, respectively.
    • JS for the logic and Operation A&&b, if A is not established (the result of the operation is False,null, "', 0,undefined equivalent, the same as) return a, if a is set to return B, if the use of && connected to multiple expressions, If each expression is true, the result of the last expression is returned, and if at least one is not true, the result of the first non-established expression
    • JS for logic or arithmetic a| | b, starting from the left, returns the result of the operation of the first expression, and returns the result of the last expression if all is not true.
    • && priority is higher than | |, the result of arithmetic && re-use && operation to | | Operation.
Look at the following code var a=0&&-1//Get 0var b=3&&4; Get 4var c=0&&1&&2; Get 0var d=1&&0&&2; Get 0var e=1&&2&&3//get 3var f=0&&false&& ' &&1//return 0var g=0| | 1; Get 1var h=1| | 0; Get 1alert (typeof (0| | false| | "); /Return Stringvar i=1| | 2&&3//return 1var j=1&&2| | 3//Return 2

JS && | | Trap JavaScript logic with, logical, or "go"

Related Article

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.