JS bool True False comparison

Source: Internet
Author: User

Think of a fun, run the following javascript:
if (' 0 ') alert ("' 0 ' is true");
if (' 0 ' = = False) alert ("' 0 ' is false"); As a result, two alert! So is ' 0 ' true or false?

The answer is: When you compare JS, there are three rules:

If there is a bool in the comparison, the bool is converted first to the corresponding number, i.e. 0 and 1
If one of the two sides of the comparison is a string, the string is converted to a number
When a string is converted directly to bool, the empty string "is converted to false, except for all strings converted to true

In the first comparison, the ' 0 ' is placed directly in the if expression, which is equivalent to converting the string directly to bool,!! ' 0 ' = = = True, so it is set up
In the second comparison, the false is converted to 0, then the ' 0 ' is converted to 0, the left and right sides are 0, so is also established.
So in the end this is a JavaScript in the time to do the comparison of the type conversion sequence of the problem, the individual think it is not very reasonable, different conversion order results will not be the same

At the time of comparison, use
Whether to publish a 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;

}

JS bool True False comparison

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.