Truthy and Falsy Values in JavaScript
In JavaScript, every value has an inherent boolean meaning when used in a boolean context. Values that evaluate to true are called truthy, and those that evaluate to false are called falsy.
There are exactly six falsy values: false, 0, "" (empty string), null, undefined, and NaN. Everything else is truthy.
console.log(Boolean("hello")); // true
console.log(Boolean(42)); // true
console.log(Boolean(0)); // false
console.log(Boolean("")); // false
console.log(Boolean(null)); // false
console.log(Boolean(undefined)); // false
Try it Yourself โ
The double negation !! is a quick way to convert any value to its boolean equivalent. It works exactly like Boolean().
console.log(!!"text"); // true
console.log(!!0); // false
console.log(!![]); // true (empty array is truthy)
console.log(!!{}); // true (empty object is truthy)
Truthy and falsy values become especially important in conditionals. Instead of comparing explicitly, you can rely on the truthiness of a value to simplify your code.