Truthy & Falsy (JavaScript)

In JavaScript, every value is either truthy or falsy when used in a condition. Falsy values are false, 0, "", null, undefined, NaN, and 0n; everything else is truthy.

JavaScript

When a non-boolean value appears in an if statement or other test, JavaScript converts it to true or false. Knowing the short list of falsy values lets you predict the result.

This is why you can write if (name) to check that a string is not empty, or if (list.length) to check that an array has items.

Examples

JavaScript

if ("") {
  console.log("runs");
} else {
  console.log("empty string is falsy");
}

Output:

empty string is falsy

See also

Back to the Dictionary