spacer

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

home / experts / javascript / column26


Changes to the Boolean Object

Developer News
ActiveState Debuts Open Source Business Suite
Salesforce Offers Visual App Builder
Codesion Steps Out From CVS's Shadow

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


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Use Web Caching to Make Your Web Site Faster · Creating an Online Shopping Cart Mechanism in PHP · Log JavaScript Errors Using an AJAX-driven Web Service
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Configuring Granular Settings for a Database Level Audit · The Perils of a Web 2.0 Transition on Your Business Processes · Facebook Redesigns Site —Again — Nears 400M Mark


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

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