truthy and falsey: javascript edition

2020-06-04

 | 

~2 min read

 | 

212 words

One of the most challenging things when getting started with Javascript is understanding comparisons.

To do so requires understanding a few things:

  1. Javascript has the concept of truthy and falsey — things that are “true-ish” and “false-ish”.
  2. There are different comparison rules that can lead to some confusing results, i.e. so called “loose equivalency”, but actually called Abstract Equality Comparison, i.e. ==, and Strict Equality Comparison, i.e. ===.

First, let’s start with the falsey values. There are six and only six:

  1. false
  2. null
  3. undefined
  4. 0
  5. NaN
  6. "" (or '' or “)

Everything else is truthy.

if (false || null || undefined || 0 || NaN || "") {
  console.log("something in the list is true")
} else {
  console.log("everything is false")
}

In the above example, you’ve probably guessed (correctly) that “everything is false” will print.

Now that that’s out of the way, let’s look at the differences between the comparison.

Despite the popular phrasing that === checks both value and type whereas == only checks type, it’s not quite right.

In fact, if you look at the spec, you’ll see the big difference is that == allows coercion of types. This is why 1 == '1' is true but not 1==='1'.


Related Posts
  • Javascript: Logical Or vs. Nullish Coalescing


  • Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!