Similar to languages such as C and Java, JavaScript can use three logical judgment symbols: &&, ||, and ! to make logical judgments on boolean values. Different from C and Java, the logical AND (&&) and logical OR (||) operators in JavaScript can be applied to any value, and the value returned after the operation is not necessarily a boolean value.
Processing rules for logic and &&
The processing rules of && in JavaScript are as follows:
1. Determine whether the first value is Falsy. If it is Falsy, the first value (not necessarily of boolean type) is returned directly.
2. If the first value is Truthy, return the second value directly (not necessarily of boolean type).
Logical OR || processing rules
Similar to the && operator, the processing rules for || in JavaScript are as follows:
1. Determine whether the first value is Truthy. If it is Truthy, the first value (not necessarily of boolean type) is returned directly.
2. If the first value is Falsy, return the second value directly (not necessarily of boolean type).
|| operator makes some shortcuts in JavaScript possible:
1. Get the first Truthy value from a series of values: