In general, & | is relatively simple. We will not discuss it here.
Prepare two objects for the following discussion.
Copy codeThe Code is as follows:
Var alice = {
Name: "alice ",
ToString: function (){
Return this. name;
}
}
Var smith = {
Name: "smith ",
ToString: function (){
Return this. name;
}
}
In javascript, for &, it can be used not only for the boolean type, but also for the return of the Boolean type.
L if the first operand is of the Boolean type and its value is false, false is returned directly.
L if the first operand is of the Boolean type and the value is true, and the other operand is of the object type, this object is returned.
L if both operands are of the object type, the second object is returned.
L if any operand is null, null is returned.
L if any operand is NaN, NaN is returned.
L if any operand is undefinded, undefined is returned.
Alert (false & alice); // false
Alert (true & alice); // alice
Alert (alice & smith); // smith
Alert (smith & alice); // alice
Alert (null & alice); // null
Alert (NaN & alice); // NaN
Alert (undefined & alice); // undefined
Alert (alice & undefined); // undefined
| Is also used not only for the Boolean type, but also for the return of the Boolean type.
In fact, null, undefined, and NaN are regarded as false. The object is treated as true.
L if the first operand is of the boolean type and the value is true, true is returned directly.
L if the first operand is of the Boolean type and the value is false, and the second operand is of the object type, the object is returned.
L if both operands are of the object type, the first object is returned.
L if both operands are null, null is returned.
L if both operands are NaN, NaN is returned.
L if both operands are undefined, undefined is returned.
Alert (False| Alice); // alice
Alert (True| Alice); // true
Alert (alice | smith); // alice
Alert (smith | alice); // smith
Alert (Null| Alice); // alice
Alert (alice |Null); // Alice
Alert (Null|Null); // Null
Alert (NaN | alice); // alice
Alert (alice | NaN); // alice
Alert (NaN | NaN); // NaN
Alert (undefined | alice); // alice
Alert (alice | undefined); // alice
Alert (undefined | undefined); // undefined
We don't need to make such a complicated recommendation. Let's take a look at this part.
A & B: Convert a and B to the Boolean type, and then execute logic and. true returns B, false returns
A | B: Convert a and B to the Boolean type, and then execute the logic or. true returns a, false returns B
Conversion rules:
The object is true.
A non-zero number is true.
The value of a non-null string is true.
Others are false.
For more information, see the following.
Js and or operator |
Js uses the and or operator priority to implement the if else condition judgment Expression
Javascript & | an alternative use technique for arithmetic operations