DEV Community

Cover image for Matt's Tidbits #98 - The truth will set you free (unless you're using JavaScript)
Matthew Groves
Matthew Groves

Posted on • Originally published at Medium

Matt's Tidbits #98 - The truth will set you free (unless you're using JavaScript)

Last time I shared a quick tidbit about running individual unit tests. This time, I want to share a few interesting things I learned while debugging a failing unit test.

If you've used JavaScript for any length of time, you may have encountered its slightly special behavior around boolean expressions. In many other languages, if statements can only contain boolean expressions - either a boolean variable, or some other expression which returns a boolean value, such as: greater than, less than, equal to, not equal to, etc.

However, in JavaScript, ALL variables themselves are inherently true/false - this is known as "truthiness", and you can view the full table here: https://developer.mozilla.org/en-US/docs/Glossary/Truthy

Where this becomes problematic is that it's SO easy to misuse this or have subtle errors in your code. In other languages, if you put a string inside of an if statement, you'll get a compile error, since it's not a boolean expression. In JavaScript though, you can do this without any errors at all - not even at runtime. Instead, every value is "coerced" to boolean. And, the rules aren't always intuitive.

The important thing to remember is that ONLY the following values evaluate to false - ALL others evaluate to true:

  • false
  • 0
  • -0
  • 0n (BigInt 0)
  • "" (empty string)
  • null
  • undefined
  • NaN (not a number)

Frankly, this list doesn't look all that bad. It kind of makes sense (don't get me started on -0 (negative zero) - that just has danger written all over it), but having an empty string evaluate to false sort of makes sense. What doesn't make sense, however, is that an empty array [] evaluates to true. C'mon guys - if you're going to have a very flexible/dynamic type system, at least make it consistent! Arrays and strings can both be null or undefined - so why is an empty string treated differently from an empty array?

The answer lies in a subtlety of the JavaScript language - strings are a special type, while arrays are objects. In JavaScript, all object references (as long as they aren't null or undefined) evaluate to true.

Anyways, as you may have gathered by now, I experienced this exact problem, where I was expecting an empty array to be false, but it was in fact true, and it took me running a unit test through a debugger to figure it out.

The moral of the story is, if you're unfamiliar/new to a particular language, make sure you carefully read (and re-read) the documentation, and always challenge your assumptions when debugging.

What are your favorite JavaScript "Truthy" stories? Let me know in the comments!

Interested in working with me in the awesome Digital Products team here at Accenture? We're hiring native mobile developers, hybrid mobile developers, web developers, and more!

Top comments (0)