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  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

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 »
7,572 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
Mar 20, 2010 at 12:07 PM
Drawing On The iPhone Canvas With jQuery And ColdFusion
Simply awesome. Saved my day. ... read »
Mar 20, 2010 at 9:00 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
I would like to say thx for an easy way to create a bottom bar. I do have a ?. Is it possible to center the bar if i want to resize it to ex 85%. Regards Offenbach ... read »
Mar 19, 2010 at 7:26 PM
MySQL 3/4 - com.mysql.jdbc.Driver And allowMultiQueries=true
Thank you very much for this post. Adding allowMultiQueries="true" in context.xml didn't help until I added it to url as allowMultiQueries=true Good idea is to use prepared statements and it will he ... read »
Jim
Mar 19, 2010 at 4:49 PM
Nobody Puts Baby In The Corner!
Wow. This is like suddenly finding a support group for your secret shame. I'm not alone! I always liked this movie, even though it is extremely cheesy. I just wish Jennifer Grey hadn't gotten the ... read »
Mar 19, 2010 at 4:47 PM
Application.cfc OnRequest() Method Affects OnError() Arguments
@Jason and @Ben, I've been doing some CF9 refactoring on our systems and noticed an odd occurrence with onError as well. Found a way to work around my problem, but what I saw was... Background: Our ... read »
Jim
Mar 19, 2010 at 4:44 PM
Shoot 'Em Up Starring Clive Owen And Paul Giamatti
I actually enjoyed this movie quite a lot. It was different, certainly, but I think they were going for more of a Quentin Tarentino-"wow, that was weird"-vibe than an actual spoof. Once I realize ... read »
Mar 19, 2010 at 4:34 PM
An Intensive Exploration Of jQuery With Ben Nadel (Video Presentation)
Hey I guess the video is down. Is there anyway you can upload to youtube or vimeo or some other service? Greatly appreciated. ... read »
Mar 19, 2010 at 4:24 PM
ColdFusion CFPOP - My First Look
@Ben Thanks for the follow up! The root of the problem had to do with being able to trace bounced emails to specific records in a DB table. Let's say you run an email campaign and you get 1,000 bou ... read »