Javascript's Implicit Boolean Conversions
Posted August 10, 2007 at 2:01 PM
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
Newer Post
Ask Ben: Environment-Based Application.cfc Settings
Older Post
Passing Referer AS ColdFusion CFHttp CGI Value vs HEADER Value?
Reader 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
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.



