Logical or (||)
var result = true || false;
Similar to logic and operations, if there is an operand that is not a Boolean, logic or not necessarily returns a Boolean; at this time, it follows the following rules:
□ If the first operand is an object, the first operand is returned.
□ If the first operand is evaluated to false, the second operand is returned.
□ If both operands are objects, the first operand is returned.
□ If both operands are null, return null
□ If both are undefined, return undefined.
□ If both are NaN, return NaN
The difference between "===" and "=="
Equality operator (==)
The equal operator will implicitly convert the operation values and compare them:
If an operation value is a Boolean, it is converted to a numeric value before comparison
If one operation value is a string and the other operation value is a numeric value, the string is converted to a numeric value through the Number() function
If one operation value is an object and the other is not, then the valueOf() method of the object is called and the result is compared according to the previous rules
null and undefined are equal
If an operation value is NaN, equal comparison returns false
If both operation values are objects, compare whether they point to the same object. If both operands point to the same object, the equal operator returns true, otherwise, return false
The following are explained separately:
Let’s talk about ===, this is relatively simple. The following rules are used to determine whether the two values === is equal:
1. If the types are different, they are [unequal]
2. If both are numerical values and are the same value, then [equal]; (!Exception) is, if at least one of them is NaN, then [not equal]. (To determine whether a value is NaN, you can only use isNaN() to judge)
3. If both are strings and the characters in each position are the same, then [equality]; otherwise [not equal].
4. If both values are true, or both are false, then [equal].
5. If both values refer to the same object or function, then [equality]; otherwise [not equal].
6. If both values are null, or both undefined, then [equal].
Let’s talk about ==, according to the following rules:
1. If the two value types are the same, make === comparison.
2. If the two value types are different, they may be equal. Type conversion is performed according to the following rules and then compared:
a. If one is null and the other is undefined, then [equal].
b. If one is a string and the other is a numeric value, convert the string into a numeric value and then compare it.
c. If any value is true, convert it to 1 and compare it; if any value is false, convert it to 0 and compare it.
d. If one is an object and the other is a numerical or string, convert the object into a value of the base type and then compare it. Convert an object to a basic type and use its toString or valueOf method. The js core built-in class will try to valueOf before toString; the exception is Date, which uses toString conversion. Non-JS core object, let's say (it's more troublesome, I don't understand much)
e. Any other combination is [unequal].
Expression Value Expression Value
null==undefinedtruetrue==1true
"NaN" ==NaNfalse$null==0false
false==0trueNaN!=NaNtrue
In short, "==" only requires equal values. "===" requires that the value and type are equal.
The information comes from JavaScript advanced programming, sorted out and updated later