What are falsy values in JS? What are truthy values in JS?


#1

Question

What are falsy values in JS? What are truthy values in JS?

Answer

A falsy value is one that evaluates to the boolean value false - expressions, statements, and values can evaluate to the false boolean value and will therefore be considered a falsy value. Some falsy values to remember:
false
0 (or other forms of the number 0, -0, 0.0, etc)
empty strings ('', "", etc)
null
undefined
NaN

Other values are considered truthy; a value that evaluates to the boolean value true - which can also be expressions, statements, and values. Some truthy values to keep in mind:
non-empty strings (like "false", ' ' - including strings with just whitespace)
-1 (or other negative and non-zero numbers)
{} (empty objects)
[] (empty arrays)
function(){}, () => {} (functions, including empty functions)

  • do keep in mind JavaScript’s type conversion when using the equality, ==, operator as this operator will convert the number 0 and/or string "0" to the same type if either is an operand of ==.
    Example: "0" == 0 //evaluates to true