Friday, April 01, 2011

Truthiness and falsiness

Stephen Colbert put the term truthiness on the map (pdf), but there is a context where the terms truthy and falsy have another, quite precise meaning. This is among people who use the programming language known as JavaScript, which runs in web browsers. The following explanation, by Mike Davies, appears on the isolani blog:
JavaScript has keywords for true and false, but like many C-style derivative languages, it has concepts of truthy and falsy. These are non-boolean expressions that can be treated as a boolean value. The number zero is falsy, and any other number is truthy. Equally for strings, an empty string is falsy, and a non-empty string is truthy.
A slight variation from another blog:
When javascript is expecting a boolean and it’s given something else, it decides whether the something else is "truthy" or "falsy". An empty string (''), the number 0, null, NaN, a boolean FALSE, and undefined variables are all "falsy". Everything else is “truthy”.
(NaN here refers to a value known as "not a number", which JavaScript returns when it needs a number but gets something else, like if you try to add 12 + "a". A discussion for another time: does boolean get a cap?)

I'm not finding a lot of references to these terms outside JavaScript. One blogger uses the terms when referring to the Clojure programming language, and I found a couple of references to falsy in some text about the language Python. Contrary to what the first cite suggests, truthy and falsy are not, at least as far as I can tell, used in descriptions of the C language or its close cousins C++ or Java.

Truthy does have a dictionary definition, which is listed as "Truthful; likely; probable." It seems to me that the JavaScript definition does not match this; in JavaScript we're not talking about likelihood or probability, just a collection of values that all are treated the same (i.e., as true).


Brad Hefta-Gaub said...

PHP also supports this concept and it also includes special versions of the comparison operators to determine if the operands are "sort of equal" or "really really equal".

So for example, in PHP:

0 == NULL
0 == false

The "==" operator, like in JavaScript compares the "truthiness" of whether or not these things are equal... it's really a "close enough" comparison.

However, in PHP we also have the "really really equal" operator. And in this case...

0===NULL will return FALSE
0===false will return FALSE

They are not "really really equal".

WordzGuy said...

@Brad -- do PHP programmers use the terms truthy and falsy to indicate the looser sorts of equality? (Or lack thereof)

Brad Hefta-Gaub said...

As for the use of the word "truthy" and "falsey" ... no, I've never heard those words used in this context... but then again, I'm also a JavaScript developer (as are all the developers I work with) and we don't really use those words to describe this in Javascript either...

Anonymous said...

I think I'm the second person to mention truthy value use in Java on blog, post: "Use truthy falsey booleans in Java". -- Josef