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:
- I speak Arabic, French, German, Spanish, and Hebrew.
- I once broke my collarbone in a game of wiffleball.
- I have been to Staten Island.
(The lie is revealed below).
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.”
- 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.
if), the conditional code wouldn’t run:
- undefined, null
- Boolean: false
- Number: -0, NaN
- String: ”
All other values are considered true, particularly all objects.
It’s even simpler in Ruby. The following are false:
- Boolean: false
Everything else is true!
Is it really that simple?
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.
Proof that this is accurate
Boolean([value]) object evaluates the Boolean value (i.e., converts non-Boolean to Boolean). In Ruby, the double bang (
(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…
- 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.
- I once broke my collarbone in a game of wiffleball. TRUE - I ran (very quickly) into someone much bigger than myself.
- 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.