What are all the values that ColdFusion considers "falsy" and "truthy"? - coldfusion

I'm looking to compile a complete list of values that ColdFusion considers falsy and truthy. The ones I know of are:
//falsy values
false
"false"
0
"no"
//truthy values
true
"true"
!= 0
"yes"
Is there anything that I'm missing here?

There's a neat little article on that here http://www.coldfusionmuse.com/index.cfm/2010/2/5/Booleans.and.Coldfusion
but of course officially it's http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec09af4-7fd0.html
In Boolean expressions, True, nonzero numbers, and the strings “Yes”, “1”, “True” are equivalent; and False, 0, and the strings “No”, “0”, and “False” are equivalent.
Boolean evaluation is not case sensitive. For example, True, TRUE, and true are equivalent.
So, using your terminology:
//falsy values
False
"False"
0
"No"
//truthy values
True
"true"
!= 0
"Yes"
"1"

Related

Print one result after combining multiple expression results using jq

I want to have the output of either true or false.
I have this syntax
.[] | (if (select(.displayName=="display")) then "yes" else "no" end)
This is the json source
[{"displayName":"display","message":"bla bla"}, {"displayName":"test","message":"bla bla"}]
I only need to query against the array and if the value im looking for exists in one of them I need the output to be "yes", and if it doesnt a "no". So a single output value is what I am looking for.
This evaluates to "yes", however, if the value display is not present in the json, it does not output "no", its just empty. Can anyone tell me why?
Here is a snippet to try: https://jqplay.org/s/WKcZh91hk8L
The idea is right, but you shouldn't be using select for this. The way select works is by evaluating the boolean expression provided inside (..) and returning the object, if the expression evaluates to true and skip the object from being considered/printed on false.
On your code, for the object that evaluates to false, select is evaluated as select(false) which as explained above returns nothing - this is not evaluated to boolean false condition. i.e. the return value of select is not a boolean value.
To use explicit boolean conditions, drop the select from expression and to summarise the result to a single value do something like below
if ( [ .[] | .displayName == "display" ] | any ) then "yes" else "no" end
Demo on jqplay
The way this works, is any can evaluate an array of boolean values and produces true as output if any of the elements of the array are true. We collect the match condition results to the array and apply any on it. i.e. [true, false] evaluates to true, thereby printing yes only once
A concise solution is possible with select:
select( any(.displayName=="display") ) | "yes" // "no"

Test answer of the boolean 'true' explaination

Im very confused why the first 3 arnt all correct. As isn't true a keyword, it of course is a Boolean literal and is interchangeable with 1?
True and False are not keywords, they are boolean values because they are associated with 1 and 0 respectively. For a complete list of keywords see Is it possible to get a list of keywords in Python?

Python 2.7 Boolean Operators Logic

I am currently in the course of learning Python 2.7 and have come across the Equality and Boolean operators
My question is:
Why False and 1 is False but True and 1 is 1
Likewise, False or 1 is 1 but True or 1 is True
Can someone kindly explain why this is happening
Many thanks
and returns the first 'falsy' (False, zero, empty string or list, etc.) value it sees, or the final value if none were falsy. Further values are not even evaluated, since they can't change the result.
or likewise returns the first 'truthy' (True, non-zero, non-empty string or list, etc.) value it sees (or the final one if there were none), and doesn't evaluate the rest.
This behavior is sometimes more convenient than strictly returning only True or False.

trying to validate string as boolean Coldfusion

I'm currently trying to validate if a string is exactly true or false as I'm building a spreadsheet importer using CF10.
My users will either enter in one of the following.
NULL, which will equate to false
TRUE which obviously equates to true
FALSE which again will equate to false.
I'm trying to use the isValid('boolean',variable.data) to validate if the data is in fact a boolean string or not. However after reading quite a few posts I can see that it will validate true if its a positive number and false if it is a negative one etc.
I'm wondering if anyone has an easy fix to getting boolean validation working for strings easily ? Is this more a regular expressions scenario to make it work properly ?
Any help greatly appreciated.
Assuming you mean the literal string "NULL", one option is using list functions. Search a list of allowed values, ie "true,false,null". If the entry is found, it is valid:
<cfif listFindNoCase("true,false,null", theValue)>
this is a valid boolean value
<cfelse>
not found. do something here...
</cfif>
Then a simple string comparison would return false for everything other than the literal string "true":
isTrue = compareNoCase(e, "true") eq 0 ? true : false`
If you need to evaluate whether a string is exactly "true" or "false", then you are not doing a boolean comparison, you are doing a string comparison.
So to that end, you'd be wanting to use compare() or compareNoCase() (depending on how rigid you need to be).
You could just do some logic or am I missing something.
<cfset booleanResult = enteredValue eq 'TRUE' ? true : false />

What Integers Are Considered "true" in a ColdFusion "if" statement [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
What are all the values that ColdFusion considers “falsy” and “truthy”?
In ColdFusion, what integers are considered true and what integers are considered false in an if statement? I know that 1 is true and 0 is false, but is this it?
For example, it would be nice to know that I can do an if on a query data structure's record count to determine if the query has at least one record in it. For example, let's say our query is called, employees.
<cfif variables.employees.recordCount>
There ARE employees.
<cfelse>
There ARE NO employees.
</cfif>
Would the statement "There ARE employees" be displayed if the record count was, say, 2?
Any non zero number is true. Only 0 will return false.
The expression inside the cfif tag must evaluate to a Boolean value (a true/false). Boolean values are represented as either "true" or "false", "yes" or "no", or "0" or any number.
Reference
So it looks like 0 is for false, or anything else for true in the case of integers.
Very similar question:
What are all the values that ColdFusion considers "falsy" and "truthy"?
Extensive blog post about how variables are evaluated for truthiness in ColdFusion:
What are all the values that ColdFusion considers "falsy" and "truthy"?