JS bool true false comparison

Source: Internet
Author: User

. For fun, run the following javascript:
If ('0') Alert ("'0' is true ");
If ('0' = false) Alert ("'0' is false"); the result is that alert is used twice! So is '0' true or false?

The answer is: when comparing JavaScript, there are three rules:

If there are bool in the comparison, the bool is first converted to the corresponding number, that is, 0 and 1.
If one of the two sides of the comparison is "Number" and the other side is "string", the string is converted to a number.
When the string is directly converted to bool, the Null String ''is converted to false, and all other strings are converted to true.

In the first comparison, put '0' directly in the IF expression, which is equivalent to directly converting string to bool ,!! '0' = true, so it is true
In the second comparison, the system first converts false to 0, then '0' to 0, and the left and right sides are both 0, which is also true.
In the final analysis, This Is A type conversion sequence problem during comparison in Javascript. I personally think it is not very reasonable. The results of different conversion sequences will be different.

Use
// Whether to release the self-service system
VaR rdopromotion = Document. getelementsbyname ("radpromotion ");
If (arraycategory [9])
{
Rdopromotion [0]. Checked = true;
Rdopromotion [1]. Checked = false;
}
If (arraycategory [9] = "false ")
{
Rdopromotion [0]. Checked = false;
Rdopromotion [1]. Checked = true;

}

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.