ColdFusion Boolean Evaluation And #( 3 + "Yes" )# = 4

Posted August 22, 2006 at 9:05 AM

Tags: ColdFusion

I always knew that numbers can be cast to boolean such as in:

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

  • <cfif Len( lstIDs )>
  • ...
  • </cfif>

But, I had no idea that boolean values can actually evaluate to numbers. This is news to me. Apparently True values are evaluated to 1 and False values are evaluated to zero. So, the equation (3 + "Yes") evaluates to (3 + 1) which is 4. Similarly, (4 + True) evaluates to 5. (False * anything) is zero.

Crazy stuff. I am not sure when I would ever use this, but I thought I would point it out in case there were any people out there like me who never knew this was an option.

One word of caution, "Yes" gets evaluated to a boolean, but Yes, might throw an error since it stands for a variable whose name is "Yes".

Download Code Snippet ZIP File

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



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

Reader Comments

Aug 23, 2006 at 9:45 AM // reply »
207 Comments

One thing that got me a few years ago - any non zero number is true, even negative #s!


Aug 23, 2006 at 10:00 AM // reply »
74 Comments

Yeah, the negative ones I get in ColdFusion, but I am very afraid to use them in Javascript... in fact, I am scared to use numbers as booleans in Javascript at all as I am not sure if it always works. I never take the time to just sit down and test it for some reason. Like, can I do:

if ("Ben".length){ ... }

Who knows? Hopefully me one day :)


Jul 11, 2008 at 11:59 AM // reply »
1 Comments

Non zero numbers (including negative) equate to True. Null strings, or in CF certain strings like "No" or "False" will return False. #( 3 + "Yes" )# = 4, but also #YesNoFormat(4)# = Yes.

It can be very useful for example if you take a string input and only want to do some action if some value was entered:

string input = getInput();
if(input){/*Put useful code here*/}

However, if("Ben".length) is like saying if(3), or if(true). In that case you might as well leave out the if statement.

This seems to be the case for any language, but of course there could be some exception to the rule.

Hope that helps


Post Comment  |  Ask Ben

Recent Blog Comments
aha
Nov 22, 2009 at 7:42 AM
Using A Name Suffix In ColdFusion's CFMail Tag
Why not? ... read »
Nov 22, 2009 at 7:37 AM
Using A Name Suffix In ColdFusion's CFMail Tag
asd ... read »
Nov 22, 2009 at 4:30 AM
jQuery Live() Method And Event Bubbling
dasegtezr ... read »
Nov 22, 2009 at 4:03 AM
jQuery Live() Method And Event Bubbling
C_fieri ... read »
Nov 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »