Conversion to a Boolean (Definition)

Converting values to a Boolean representation.

There are three conversions to study. Obviously Boolean values will remain as they are.

This table summarizes the effects of converting values to Boolean:

Value:Boolean equivalent:
nullfalse
Undefined valuefalse
Non empty stringtrue
Empty stringfalse
0false
NaNfalse
Infinitytrue
Negative infinitytrue
Any other non zero numbertrue
Objecttrue
ArrayNo direct boolean equivalent
Functiontrue

There is no proper Boolean equivalent for an array. The ECMA standard does not address this either. The standard does suggest that any non-null object reference convert to true and since an array is an object, it should rightly become a true value.

Warnings:

Example code:

   <!-- Is this a bug or a feature??? -->

   <HTML>

   <HEAD></HEAD>

   <BODY>

   <SCRIPT>

   myTrueObject = new Boolean(false);

   if(myTrueObject)

   {

      alert("True");

   }

   else

   {

      alert("False");

   }

   </SCRIPT>

   </BODY>

   </HTML>