Nerd For Tech
Published in

Nerd For Tech

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, 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 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 — ALL others evaluate to

  • 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 (negative zero) — that just has danger written all over it), but having an empty string evaluate to sort of makes sense. What doesn’t make sense, however, is that an empty array evaluates to . 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 or — 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 or ) evaluate to .

Anyways, as you may have gathered by now, I experienced this exact problem, where I was expecting an empty array to be , but it was in fact , 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!

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store