Skip to content
davidchambers edited this page Jul 16, 2012 · 9 revisions

Falseyness

JavaScript, it is well known, has a strange notion of falseyness. So when I was designing PogoScript I wondered if there would be an appetite for it being less strange.

In particular, I thought Ruby had the nicest semantics for falseyness, that is, only nil and false itself were false and nothing else.

In fact every language has a different notion of falseyness, which surprised me because having written plenty of code in plenty of languages, I thought I would have noticed this.

So I looked into a number of other languages and found some surprising results. What I was looking for were values that would not pass as a true condition to an if statement/expression.

Lisp: nil and the empty list (), which are actually the same thing in Lisp. Lisp doesn't have a "false".

Scheme: #f. Unlike Lisp, the empty list is not false, interestingly Scheme doesn't have a nil.

JavaScript: empty string "", 0, null, undefined and false.

Ruby: false and nil.

Python: empty string "", 0, False, None, and empty collections (such as [] and {}).

C/C++: NULL and FALSE which are really just macros for 0.

Objective C: same as C/C++, but also defines NO as false.

C# and Java: false. Not even null is allowed in an if statement.

Go: false. Same as C# and Java.

Smalltalk: false.

Personally I like Ruby's, it offers a nice cross between Smalltalk and Lisp. But given that some very popular languages are also weird, and that PogoScript is running on JavaScript I think I'll stick to JavaScript semantics.

Clone this wiki locally