A detailed explanation of Boolean usage in JavaScript

Source: Internet
Author: User

The Boolean value true represents True and false for false. The generic relational operator returns the result of a Boolean value. In addition, values of 0,-0, NULL, NaN, undefined, and null characters ("") of special values are interpreted as false, and other values are interpreted as true.

function Ismonth (Mon) {
if ((Mon >= 1) && (Mon <= 12)) {
return true;
} else {
return false;
}
}
if (Ismonth (Mon)) {
Alert ("OK");
} else {
Alert (Please enter the correct month.) ");
}

Boolean = new Boolean (value)

Generates a Boolean object. Set value to the initial value true or FALSE. In order to conform to the idea that "data types can generate objects" in object-oriented form, JavaScript prepares this class, but it is basically useless.

xx = new Boolean (true);

Example

It is useful to use the Boolean value true false to determine the checked of input to cancel and select all check boxes.

Js:

Window.onload=function ()
{
var ainput=document.getelementsbytagname ("input");
Ainput[0].onclick=function ()
{
for (Var i=3;i<ainput.length;i++)
{
Ainput[i].checked=true;
}
};
Ainput[1].onclick=function ()
{
for (Var i=3;i<ainput.length;i++)
{
ainput[i].checked=!ainput[i].checked;
}
};
Ainput[2].onclick=function ()
{
for (Var i=3;i<ainput.length;i++)
{
Ainput[i].checked=false;
}
};
};

Html:

<input type= "button" value= "Select All"/>
<input type= "button" value= "anti-election"/>
<input type= "button" value= "not selected"/>
<br/>
<input type= "checkbox"/>
<br/>
<input type= "checkbox"/>
<br/>
<input type= "checkbox"/>
<br/>
<input type= "checkbox"/>
<br/>
<input type= "checkbox"/>
<br/>
<input type= "checkbox"/>
<br/>
<input type= "checkbox"/>
<br/>
<input type= "checkbox"/>
<br/>
<input type= "checkbox"/>
<br/>
<input type= "checkbox"/>

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.