CodeNewbie Community 🌱

Discussion on: What's the difference between boolean values and truthy and falsy values?

Collapse
 
denmch profile image
Den McHenry • Edited

Under the hood, JS will resort to conversion tables to try to resolve data types and keep humming. This can be tricky and make for lots humorous outcomes, like when you accidentally "add" a string to an integer and end up concatenating two strings so that "1" + 1 becomes "1" + "1" and you end up with 11. Similarly, certain values will resolve as true or false under the hood, so that when JS wants a Boolean value (true or false), but sees something else, it needs to convert it. If the input converts to true, the value is considered truthy, and if it converts to false, it's considered falsy.

I came up with a mnemonic a few years ago to remember the falsy values. Anything else is truthy. The mnemonic is "Falsy Values are N0 FUN!"

falsy values == N0 FUN

N0:

  • NaN
  • 0 or -0 or "" (empty string, i.e., String.length === 0)

FUN:

  • false
  • undefined
  • null