Chris EK, on life as a continually learning software engineer.

Two Truths and a [False]: Booleans and Truthiness in Ruby and JavaScript

We all know the icebreaker game “Two Truths and a Lie”: each individual in a group tells three statements about themselves; the rest of the group tries to distinguish which of the three is a lie. For example:

  1. I speak Arabic, French, German, Spanish, and Hebrew.
  2. I once broke my collarbone in a game of wiffleball.
  3. I have been to Staten Island.

(The lie is revealed below).

Context in Programming: Ruby vs. JavaScript

What’s Boolean? Per Wikipedia, “the Boolean or logical data type is a data type, having two values (usually denoted true and false), intended to represent the truth values of logic and Boolean algebra. It is named after George Boole, who first defined an algebraic system of logic in the mid 19th century.”

Some context in JavaScript:

JavaScript makes a somewhat arbitrary distinction between values:

  • The primitive values are Booleans, numbers, strings, null, and undefined.

  • All other values are objects.

This distinction between primitives and objects is important, as we’ll see in a minute.

What’s False in JavaScript?

JavaScript interprets the following values as false (i.e., if these values were to follow a conditional (if), the conditional code wouldn’t run:

  • undefined, null
  • Boolean: false
  • Number: -0, NaN
  • String: ”

All other values are considered true, particularly all objects.

And Ruby?

It’s even simpler in Ruby. The following are false:

  • nil
  • Boolean: false

Everything else is true!

Is it really that simple?

Yes and no. In terms of how Ruby and JavaScript evaluate certain values, yes. Where a Boolean is expected, Ruby evaluates nil as false, JavaScript evaluates NaN as false.

That said, what we’re doing is evaluating non-Boolean values as Boolean. For that reason, values like 8, “abc”, undefined, and nil are “truthy” or “falsey”. They’re not technically true or false, because only true and false are Booleans.

Note: “In some languages, like C and Python, 0 counts as false. In fact, in Python, empty arrays, strings, and hashes all count as false. In JavaScript, empty strings count as false, but empty arrays count as true.” Again, in Ruby, only nil and false evaluate as false. Even 0 evaluates as true.

Proof that this is accurate

The best way to become familiar with Boolean values is to try them out in the console. In JavaScript, the Boolean([value]) object evaluates the Boolean value (i.e., converts non-Boolean to Boolean). In Ruby, the double bang (!!) converts non-Boolean values to Boolean. See some examples below: first JavaScript, then Ruby.

(note, that warning—“string literal in condition”—is telling us what we already know: that a string literal like “abc” is non-Boolean but being evaluated as such.)

My lie revealed

For those readers interested in my personal truths and lies…

  1. I speak Arabic, French, German, Spanish, and Hebrew. FALSE[Y] - I speak (to some degree) Arabic, French, German, and Spanish. I do not speak Hebrew.
  2. I once broke my collarbone in a game of wiffleball. TRUE - I ran (very quickly) into someone much bigger than myself.
  3. I have been to Staten Island. TRU[THY] - I haven’t really been to Staten Island, but I’ve taken the ferry a few times and driven across the Verrazano Bridge.