Hiprup

What are falsy and truthy values in JavaScript?

In boolean contexts, every value is either truthy or falsy.

  • Falsy values — exactly eight: false, 0, -0, 0n, "", null, undefined, and NaN.

  • Truthy — everything else, including "0", "false", [], and {}.

Watch out: empty arrays and objects are truthy — to check emptiness, test length or keys.

// Falsy values (all 6)
console.log(Boolean(false));     // false
console.log(Boolean(0));         // false
console.log(Boolean(''));        // false
console.log(Boolean(null));      // false
console.log(Boolean(undefined)); // false
console.log(Boolean(NaN));       // false

// Truthy surprises
console.log(Boolean([]));        // true! (empty array)
console.log(Boolean({}));        // true! (empty object)
console.log(Boolean('0'));       // true! (non-empty string)
console.log(Boolean('false'));   // true! (non-empty string)
console.log(Boolean(-1));        // true! (non-zero number)

// Practical: conditional checks
if (userInput) { /* truthy — has value */ }
if (!userInput) { /* falsy — empty/null/undefined */ }

// Double negation for boolean conversion
console.log(!!0);     // false
console.log(!!'hello'); // true
console.log(!!null);   // false

Only 6 values are falsy — everything else is truthy. Empty arrays and objects are truthy (common surprise). '0' and 'false' are truthy (non-empty strings). !! converts any value to its boolean equivalent.

These rules affect if statements, &&, ||, and ternary operators.

Memorize the 6 falsy values: false, 0, '', null, undefined, NaN. EVERYTHING else is truthy (including [], {}, '0', 'false').

The empty array being truthy is the most tested surprise. !! for boolean conversion.

What are falsy and truthy values in JavaScript? | Hiprup