Truthy (JavaScript)

Learned about this in CS349.

https://www.sitepoint.com/javascript-truthy-falsy/

As well as a type, each value also has an inherent Boolean value, generally known as either truthy or falsy. Some of the rules that determine how non-Boolean values are translated into true or false values are a little bizarre. Understanding the concepts and their effect on comparison helps when debugging JavaScript applications.

The following values are always falsy:

  • false
  • 0 (zero)
  • -0 (minus zero)
  • 0n (BigInt zero)
  • ''""`` (empty string)
  • null
  • undefined
  • NaN

Everything else is truthy. That includes:

  • '0' (a string containing a single zero)
  • 'false' (a string containing the text “false”)
  • [] (an empty array)
  • {} (an empty object)
  • function(){} (an “empty” function)