It is a cliche that 0 in js is true or false
- 2021-07-26 06:48:40
- OfStack
Think of a fun one and run the following javascript:
if ('0') alert("'0' is true");
if ('0' == false) alert("'0' is false");
As a result, both alert! So is' 0 'true or false?
The answer is: When comparing js, there are three rules:
If there is bool in the comparison, bool will be converted into the corresponding number, that is, 0 and 1
If number1 and string are compared, string will be converted into a number
When converting string directly to bool, the empty string ''is converted to false, and the other 1-cut string is converted to true