spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column26


Changes to the Boolean Object

Developer News
Google Going Native With Chrome
Mozilla Fixes Firefox Flaws as 3.5 Release Nears
Microsoft and Novell Still Bosom Buddies

JavaScript 1.3 changes the way a Boolean object is treated in conditional statements. If you use Boolean objects in your code, you'll better verify that they are used in a 1.3-compliant manner.

In JavaScript 1.2, a Boolean false object was treated as false in conditional tests. Likewise, a Boolean true object was treated as true. In JavaScript 1.3, though, a Boolean object is always treated as true. Let's look at the simplest if statement's syntax, if(condition). When the condition is a Boolean object, JavaScript 1.3 returns a different value than JavaScript 1.2 returned. JavaScript 1.2 returned the value of the Boolean object. If the Boolean object was a false object, the if statement was evaluated to false. If the Boolean object was a true object, the if statement was evaluated to true. JavaScript 1.3 does not care about the value of the Boolean object. It always returns true if the Boolean object exists. In fact, it returns true for any object type, as long as it exists.

The following piece of JavaScript code will yield different results in 1.2 than in 1.3. The 1.2 version:

<SCRIPT LANGUAGE="JavaScript1.2">
a= new Boolean(false);
if (a) document.write("The value of a is " + a);
</SCRIPT>

does not print the message, because the if statement returns the value of the object which is false. The 1.3 version:

<SCRIPT LANGUAGE="JavaScript1.3">
a= new Boolean(false);
if (a) document.write("The value of a is " + a);
</SCRIPT>

does print the message, because the if statement only checks for the existence of the Boolean object and returns true. Notice these JavaScript 1.3's changes affect only user-defined Boolean objects. The behavior of conditional tests have not been changed. For example, tricks for checking the browser identity will continue to work. The following two-line script:

IE4 = (document.all);
if (IE4) alert("This message for IE4"); 

will not print its message under Navigator, because IE4 is false.

http://www.internet.com

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
XML and PHP Simplified · Creating a ASP.NET Contact Form · Data Filtering with PHP
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Intel to Host Live Nehalem Q&A · 12 Tips to Troubleshoot Network File-Sharing · 10 Tips for Selling on Kijiji


Created: September 28, 1998
Revised: September 28, 1998

URL: http://www.webreference.com/js/column26/boolean.html