Small mistakes in JavaScript-------common error four: comparison operators

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.