General && and | | It is relatively simple and is not discussed here.
Prepare two objects for the following discussion.
The code is as follows:
var Alice = {
Name: "Alice",
Tostring:function () {
return this.name;
}
}
var Smith = {
Name: "Smith",
Tostring:function () {
return this.name;
}
}
in JavaScript, && is not just for a Boolean type, but for the result of a Boolean type.
L returns false directly if the first operand is of type Boolean and the value is false.
If the first operand is of type Boolean, and the value is true, and the other operand is of type object, the object is returned.
L If two operands are of type object, then the second object is returned.
L If any one of the operands is NULL, then NULL is returned.
L If any one of the operands is Nan, then the Nan is returned.
L If any one operand is undefinded, then return undefined.
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
for | | , it is not just for the Boolean type, but also for returning the result of the Boolean type.
In fact, null, undefined, and NaN will all be treated as false. And the object is treated as true.
L Returns True if the first operand is of type Boolean and the value is true.
If the first operand is of type Boolean and the value is false and the second operand is object, the object is returned.
L returns the first object if all two operands are of type object.
L If two operands are null, then NULL is returned.
L If the two operands are Nan, then the Nan is returned.
L If two operands are undefined, then return to undefined.
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
&& in Javascript | | Use summary