So today I learned something
true, even with the not-at-all-precise
#What is truthy?
Truthy isn’t some hand-wavy word that I came up while blogging after drinking. It actually has a specific meaning. Here’s the definition according to MDN:
truewhen evaluated in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for
What this means in practice is that if you use a truthy thing like the string “flying-whale” as the condition for an
while statement, then the condition will pass.
#Truthy ain’t true
If flying whales are truthy, then the obvious question becomes: are they also true? Take a look at this example’s console output to find out.
"falling-petunias" to be
true either. But I guess it can’t take all of its design decisions from the hitchhiker’s guide.
if statements actually work surprised me. I thought that they checked if the expression was
#Counting flying whales
"flying-whales" does not equal
true. And you’d expect that numbers (representing counts of flying whales) won’t be true either.
2 flying whales won’t be true; the very idea of multiple whales flying around at the same time is outlandish! But what about
1 flying whale?
If you don’t want to go to the trouble of reading my wonderful algorithmically generated folk song, then here’s the gist of it:
2 == trueevaluates to
1 == trueevaluates to
Control statements like
while are also loose, but in a completely different way to the
== operator. They check whether something is truthy, which is a much broader categorization than
But what if you want to check if something is actually
true? Then you need to use strict equality.
#Equality without coercion
I also realized today that there must be a lot of politically minded computer scientists out there naming these things.
coercion The action or practice of persuading someone to do something by using force or threats.
— The Oxford English Dictionary
==, which you’ve already seen, and the strict equality operator
==, because it doesn’t do any coercion. It only evaluates to
true if both sides are exactly equal… unless both sides are
NaN, in which case it still evaluates to
false. And there’s actually a good reason for this: that’s how floating point numbers work in other languages too!
#A veritable minefield
== true are different things today. But what brought on that realization?
Earlier today, I had a go at this wonderful little minesweeper-esque game. After submitting my answer, I was like “huh?” when some of the cells didn’t display either ticks or crosses. Bartosz Szczeciński kindly explained why (thanks!) — it’s only checking that you correctly mark cells that are loosely equal using
I found the topic (and the game) interesting, and wanted to pay the explanation forward with this article. Since you’ve read this far, maybe you’d like to share it forward too? If so, there’s a few buttons on the left to make it easier. Either way, thanks so much for reading!
If you have any questions or comments, give me a shout anytime at email@example.com!