What truthy and falsy mean

An if statement needs a yes-or-no decision, but its condition does not have to be a boolean. JavaScript converts a non-boolean value to a boolean for that decision. A value that converts to true is called truthy; one that converts to false is falsy. For example, if ("ready") { console.log("Go"); } prints Go because a nonempty string is truthy. By contrast, if ("") { console.log("Go"); } does not enter the block.

You can inspect this conversion directly with Boolean(value). Try the examples in the console of a modern browser or in Node.js 18 or later; no packages are needed. Boolean("ready") evaluates to true, while Boolean("") evaluates to false. These examples use ordinary JavaScript syntax and do not require a web page.

Which values are falsy?

The falsy values you are most likely to encounter are false, 0, -0, 0n (the BigInt zero), NaN, "" (an empty string), null, and undefined. NaN is a special numeric value often produced when a calculation cannot yield a valid number. Aside from these values, ordinary JavaScript values are truthy.

Notice that truthiness does not describe what a value means to a person. The strings "0" and "false" are truthy because they contain characters. A string containing only a space is truthy too. If text comes from a form field, if (input) distinguishes an empty string from a nonempty one, but it does not tell you whether the text is useful. To reject whitespace-only input, you could check input.trim() instead, provided input is a string.

Why empty arrays and objects still pass

Empty collections are a common surprise. Both Boolean([]) and Boolean({}) evaluate to true. An array or object remains truthy even when it contains nothing; its contents are not inspected during boolean conversion. That means if (items) checks whether items is truthy, not whether an array has any entries.

If you expect an array and want to know whether it contains entries, use if (items.length > 0). If items might be missing, check before reading length: if (Array.isArray(items) && items.length > 0) { console.log("Has items"); }. The second check runs only when Array.isArray(items) is true, so it avoids reading length from a missing value. For a plain object, Object.keys(record).length > 0 can check for enumerable own string-keyed properties, but use it only when that definition of “nonempty” fits your task.

Choose the check that matches your question

A truthiness check is useful when every falsy value should take the same path. For instance, if (!message) { console.log("No message"); } handles an empty string, null, and undefined alike. It also treats 0 and false as absent, though, so it may be a poor choice for a field that legitimately holds those values.

When you need a particular distinction, make it explicit. Use count === 0 to check for numeric zero, name === "" to check for an empty string, or enabled === false to check for the boolean false. The strict equality operator === does not convert different types to make them match: 0 === "0" is false. If you want to accept either null or undefined as a missing value, value == null is a concise special case; value === null || value === undefined spells it out without loose equality.

Watch for truthiness in default values

Truthy and falsy values matter outside if statements. The || operator returns its left operand when that operand is truthy; otherwise, it returns its right operand. Thus 0 || 10 evaluates to 10. That can be useful for a fallback, but it can also replace a valid zero. The ?? operator uses its right operand only when the left is null or undefined, so 0 ?? 10 evaluates to 0.

Before writing a conditional or fallback, ask what should happen for zero, an empty string, false, null, and undefined. If those cases need different treatment, an explicit comparison is usually easier to understand than relying on truthiness. Use truthiness as a deliberate shorthand, not as a substitute for deciding what your data means.

Back to category ↗