small mistakes in JavaScript-------common error Four: comparison operators
one of the more convenient parts of JavaScript is the ability to force each result variable in a comparison operation into a Boolean type. But on the other hand, sometimes it can bring us a lot of inconvenience, and here are some examples of code that have been bothering many programmers:
Console.log (false = = ' 0 ');
Console.log (Null = = undefined);
Console.log ("\t\r\n" = = 0);
console.log (' = = 0);//And these do too!
if ({})//...
if ([])//...
the last two lines of code, although the condition is judged to be null (often mistaken for false), but in fact, whether {} or [] is an entity class, and any class will actually be converted to true. As these examples show, some types of forced conversions are very vague. So many times we prefer to use = = = and!== instead of = = and! = To avoid forced type conversions. The usage of = = = and!== is the same as the previous = = and! =, except that they do not take type casts. It is also important to note that when any value is compared to NaN, even including himself, the result is false. Therefore, we cannot use simple comparison characters to determine whether a value is NaN. We can use the built-in IsNaN () function to identify:
Console.log (nan = = Nan);//False
Console.log (nan = = = Nan);//False
Console.log (IsNaN (NaN));//True
Small mistakes in JavaScript-------common error four: comparison operators