Javascript's Implicit Boolean Conversions

Posted August 10, 2007 at 2:01 PM

Tags: Javascript / DHTML

In ColdFusion, I am very confident about what values will be implicitly converted to true / false boolean values. In Javascript, however, my confidence is much lower. I know that objects will be false if they are "null", but what about things like numeric values? I end up writing code like this:

 Launch code in new window » Download code as text file »

  • if (strValue.length > 0){ ... }

... because I'm never sure how "strValue.length" will be used without explicit comparisons. This not only demonstrates a lack of understanding in the language, it also means excess code and a lower signal-to-noise ratio. Shame on me!

Well, not no more! Now, I am finally testing all of these values to check the implicit boolean value conversions that Javascript will do for me:

 Launch code in new window » Download code as text file »

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html>
  • <head>
  • <title>Javascript True / False Testing</title>
  • </head>
  • <body>
  •  
  • <script type="text/javascript">
  •  
  • // Short hand for writing to document.
  • function O( strValue ){
  • document.write(
  • "<p>" + strValue + "</p>"
  • );
  • }
  •  
  • // Short hand for testing boolean.
  • function B( objAny ){
  • // Test argument for true / false,
  • if (objAny){
  • return( "TRUE" );
  • } else {
  • return( "FALSE" );
  • }
  • }
  •  
  • // Test true value.
  • O( "true: " + B( true ) );
  • O( "false: " + B( false ) );
  • O( "null: " + B( null )) ;
  • O( "Zero: " + B( 0 ) );
  • O( "Positive Num: " + B( 1 ) );
  • O( "Negative Num: " + B( -1 ) );
  • O( "String Length: " + B( ("test").length ) );
  • O( "Zero String Length: " + B( ("").length ) );
  • O( "Array Length: " + B( ([1]).length ) );
  • O( "Zero Array Length: " + B( ([]).length ) );
  • O( "Object: " + B( {a:1} ) );
  • O( "Array: " + B( [] ) );
  • O( "Zero AND true: " + B( 0 && true ) );
  • O( "Zero AND One: " + B( 0 && 1 ) );
  • O( "1 AND -1: " + B( 1 && -1 ) );
  •  
  • </script>
  •  
  • </body>
  • </html>

Running the above code, we get the following output:

true: TRUE

false: FALSE

null: FALSE

Zero: FALSE

Positive Num: TRUE

Negative Num: TRUE

String Length: TRUE

Zero String Length: FALSE

Array Length: TRUE

Zero Array Length: FALSE

Object: TRUE

Array: TRUE

Zero AND true: FALSE

Zero AND One: FALSE

1 AND -1: TRUE

I think the biggest confidence builder here is that fact that the number zero is considered false and that any NON-zero number (positive or negative) is considered true. This will make my code much more readable (to me). Sweet! That's what happens when you go ahead and test stuff - you get rid of the mystery.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Reader Comments

Aug 13, 2007 at 6:44 AM // reply »
1 Comments

Save even more chars:
Empty string ("") is also false.
Btw, you may use "double negation" instead of "if" statement to convert any value to boolean:

var bool = !!someVar;

So
!!0 == false;
!!1 == true;
!!"ABC" == true;
!!"" == false;

Valery


Aug 13, 2007 at 7:18 AM // reply »
6,516 Comments

Empty string is false. Very interesting. I am not sure exactly how I feel about that, but lo and behold, I just tested it and you are dead on the money. I like it, thanks for the hot tip.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »