Skip to content

Truthy and Falsy Values

Any JavaScript value can be evaluated as true or false in a boolean context (like an if condition), even if it isn’t actually a boolean. Only a specific, fixed set of values are falsy: false, 0, -0, "", null, undefined, and NaN. Every other value β€” including "0", [], and {} β€” is truthy.

if ("") console.log("truthy"); // doesn't run -- "" is falsy
if ("0") console.log("truthy"); // runs -- non-empty string is truthy!
if (0) console.log("truthy"); // doesn't run -- 0 is falsy
if ([]) console.log("truthy"); // runs -- empty array is truthy!
if ({}) console.log("truthy"); // runs -- empty object is truthy!
const name = "";
console.log(name || "Guest"); // "Guest" -- falls back because "" is falsy
console.log(Boolean(name)); // false -- explicit conversion
console.log(!!"hello"); // true -- double-negation converts to boolean
console.log(!!0); // false

Assuming an empty array or object is falsy β€” both [] and {} are truthy in JavaScript, unlike some other languages, so if (myArray) doesn’t tell you whether the array has items.

const items = [];
if (items) {
console.log("has items"); // runs! [] is truthy, regardless of length
}
// Correct way to check for an empty array
if (items.length > 0) {
console.log("has items"); // correctly does NOT run
}
  1. List the seven falsy values in JavaScript.

    Answerfalse, 0, -0, "", null, undefined, and NaN.
  2. Is an empty array [] truthy or falsy?

    AnswerTruthy β€” arrays and objects are always truthy in JavaScript, even when empty.
  3. What does !!value do?

    AnswerConverts value to its explicit boolean equivalent β€” the first ! negates and coerces to boolean, the second ! negates it back to the "real" truthy/falsy value.