« The True Explanation of Bigfoot | Main | JavaScript Types and Conversions »

Truthy and Falsey

I was originally going to include this topic in the previous post about types and conversion, but I went on so long about that one that I thought this should be separate. This was also semi-related to something I believe I wrote about a few weeks ago. I've had abysmal luck with writing lately, so I'm not sure if it made it past the drafting stage. I typically prefer to write offline, then post the results. The last 3 or 4 times I've tried to do this, I've ended up losing the draft before saving it. A rational person might learn from this example, and perhaps save more often, but I seem immune to that type of rationality. I think it's because these posts are rarely given a lot of thought or planning ahead of time. Something strikes my fancy, then I brain-dump it all into a text editor and paste it here. It works (except, you know, when it doesn't) so why mess with a good thing?

The topic today is truthy and falsey values. Now this sounds like cutesy jargon, but I think it's just the easiest way to refer to these things. This is partly a segue topic to talk about a deeper understanding of programming I think I'm acquiring. Truthy and falsey refer to values that evaluate to true or false, but are not actually compared to anything. 1 == 2 is obviously false, but what if you just evaluate 1? It evaluates to true, which makes it truthy. In JavaScript (and this might hold true for PHP as well) almost everything is truthy. The only stuff that isn't truthy is 0, empty strings, undefined, null, and NaN. This stuff is good for quick tests to see if something you're expecting in the code is actually there. You can do an if test on that variable or object or whatever, and it evaluates to true then it's there and ready for use. If it evaluates to false, then something went wrong. That's a simple example, but it's stuff like this that lets you start to get more clever with your programming. If I'm writing something complex, and I keep in mind that the supporting code should give back a value if it succeeds or nothing if it fails I can easily test that later on. There are probably better examples, but a lot of this is still theoretical for me.

What this leads into is I had a realization that I was doing this already, but I didn't really understand it. I've been trying to think of a way to describe it, but the only thing I can come up with is the difference between using a key to open a lock, and understanding how the tumblers are cut and the lands and grooves on a key make them line up enough to turn the cylinder. I never really needed to know it. I knew it probably from looking at other code when trying to figure out a problem, and it made a certain intuitive sense. It's interesting when these connections happen. It really gives a deeper appreciation for what goes into writing this code. It's also a huge confidence builder when you're not guessing at what something will do. You know how the system works, and you know that if you're doing this thing, it will work like this. And if it doesn't work like that, you know where to look for an error. I've found this a lot when starting a new language, or learning some new technique. This happened with me when I first started adding SQL content to projects, where it took forever to get all the particulars ironed out. But the SECOND it works all that self doubt evaporates. All those pieces that were so confusing click into place and the next time you have to do it the whole process is so much easier.

Now I'm not saying I've had that "it all clicked into place" moment with JS. Far from it. But the truthy and falsey thing is an example of one of those times where a lot of stuff started making more sense. It's a good feeling, and I hope there's more of it coming.

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (2)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    Response: Andrew Ting MD
    Rich Burns - Journal - Truthy and Falsey
  • Response
    Response: Andrew Ting MD
    Rich Burns - Journal - Truthy and Falsey

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
All HTML will be escaped. Hyperlinks will be created for URLs automatically.