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

Posted August 22, 2006 at 9:05 AM by Ben Nadel

Tags: ColdFusion

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

• <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".

### Looking For a New Job?

25% of job board revenue is donated to Kiva. Loans that change lives - Find out more »

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

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 »

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 »

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

May 13, 2010 at 10:15 PM // reply »

It comes in extremely handy when you are setting query or sproc parameters and need to set a null for a empty string.
Example:
<cfprocparam cfsqltype="CF_SQL_VARCHAR" value="#someString#" null="#yesNoFormat(not Len(someString))#">

May 14, 2010 at 9:01 AM // reply »

@Judah,

Word up to that! And I'll do you one more - I don't believe you actually need the yesNoFormat() function. I believe the "#not len()#" should be sufficient.

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

 Author Name: Author Email: Author Website: Comment: Supported HTML tags for formatting: bold   italic   code Remember my information Subscribe to comments Send me a copy of this comment
InVision App - Prototyping Made Beautiful With Prototyping Tools Recent Blog Comments
Mar 10, 2014 at 8:24 PM
Nested Views, Routing, And Deep Linking With AngularJS
@Steven, the action property is not something from AngularJS but rather part of the concept that Ben is describing in his blogpost here. it is a custom added property, which is read by the requestCo ... read »
Mar 10, 2014 at 2:03 PM
Nested Views, Routing, And Deep Linking With AngularJS
Where is the angular documentation for 'action:' it is not here: http://docs.angularjs.org/api/ngRoute/provider/ \$routeProvider Thanks. ... read »
Mar 10, 2014 at 12:06 PM
Using Track-By With ngRepeat In AngularJS 1.2
I was hoping that this will work with pagination using ng-repeat. My use case scenario is that I have an images object. images[0] = [im1,im2,im3] // First Page images[1] = [im4, im5, im6] // Second ... read »
Mar 9, 2014 at 6:11 PM
For Better Security Use HtmlEditFormat() In Conjunction With JSStringFormat() In ColdFusion
It looks like htmleditformat() will be deprecated in CF 11 https://wikidocs.adobe.com/wiki/display/coldfusionen/New +in+ColdFusion ... read »
Mar 9, 2014 at 10:55 AM
\$.stop() vs. \$.finish() In jQuery Animations
Nice feature! Thanks for sharing. :) Good for when you are making a 100% AJAX controlled site. ... read »
Mar 9, 2014 at 12:26 AM
Experimenting With Multiple Class Inheritance In Javascript
@bigboomshakala, Hi, I am want present so solution for multiple prototypical inheritance, but some restrictions. Please follow the link: http://arto8.site11.com/ArtoJS/multi-inheritance-prototype.ph ... read »
Mar 8, 2014 at 5:21 PM