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"