Matt's Tidbits #98 - The truth will set you free (unless you're using JavaScript)
Tags: mattstidbits, javascript, tidbittuesday
Published: 2021-06-29 12:59:19

[Last time I shared a quick tidbit about running individual unit tests.](https://medium.com/nerd-for-tech/matts-tidbits-97-running-debugging-individual-jest-unit-tests-36f7464fbaf9) 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](https://www.accenture.com/us-en/careers/jobdetails?id=00960587_en&title=Native+Mobile+Developer), [hybrid mobile developers](https://www.accenture.com/us-en/careers/jobdetails?id=00960583_en&title=Hybrid+Mobile+Developer), [web developers](https://www.accenture.com/us-en/careers/jobdetails?id=00906120_en&title=Web+Developer), and [more](https://www.accenture.com/us-en/careers/jobsearch?jk=%22Digital%20Products%22&sb=0&pg=1&is_rj=0&ct=ma%20-%20cambridge|ny%20-%20new%20york)!