Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

What does truthy or falsy mean in JavaScript?

Quick answer

In JavaScript, a value is truthy if a conditional treats it as true, and falsy if the conditional treats it as false. The standard falsy values are false, 0, -0, 0n, "", null, undefined, and NaN; browsers also have the special-case document.all. Empty arrays and empty objects are truthy, so use an explicit length check when you mean “not empty.” [S1][S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

What does truthy or falsy mean? I keep seeing these terms in JavaScript examples.

Geez

They describe how JavaScript interprets a value when a condition needs a Boolean result. For example, if (value) runs its block when value is truthy and skips it when value is falsy. JavaScript performs this Boolean conversion automatically in a conditional; the original value does not have to be the Boolean true or false. [S1][S2]

That distinction matters: if ("hello") runs, but the string itself is still a string. Truthiness is a convenient way to make a yes-or-no decision, not a test that a value literally equals true.

Follow-up question

Which values should I recognize as falsy?

Geez

The standard falsy values are:

  1. false
  2. 0 and -0
  3. 0n (BigInt zero)
  4. "" (the empty string)
  5. null
  6. undefined
  7. NaN

MDN also lists document.all as a browser-specific falsy value. It is unusual; the standard list above covers the common cases. [S1]

A string containing a space, such as " ", is not an empty string, so it is truthy. Likewise, "0" is a nonempty string and is truthy. Those distinctions can matter when values come from form fields or other text input. [S1]

Follow-up question

I expected an empty array or object to count as empty. Why does an if check accept them?

Geez

Because truthiness does not test whether a collection contains items or properties. Empty arrays and empty objects are truthy, just like nonempty ones. [S1][S2] So this runs its block even when the array has no entries:

const items = [];
if (items) {
  console.log("This runs");
}

If you mean “the array has at least one item,” check items.length > 0. That expresses the condition you intend; a plain if (items) only checks whether the value is truthy.

Follow-up question

Can you give me a small snippet that prints the results, and a rule of thumb for choosing a check?

Geez

This example prints each value and the Boolean result of applying Boolean to it. It includes common falsy cases, empty containers, and values that can look empty:

const samples = [
  false, 0, -0, 0n, "", null, undefined, NaN,
  " ", "0", [], {}, ["item"], { name: "Ada" }
];

for (const value of samples) {
  console.log(value, Boolean(value));
}

The empty string and listed zero or missing-value cases produce false; the space string, the string "0", the empty array, and the empty object produce true. The example demonstrates Boolean conversion; it does not check whether an array or object has contents. [S1][S2]

Rule of thumb: use truthiness when any falsy value should mean “no.” Use a specific check when you mean something narrower, such as “not an empty string,” “a number other than zero,” or “an array with entries.” For example, items.length > 0 clearly expresses the last intention.

A practical checklist

  • When reading if (value), identify which falsy values might occur in that variable. [S1]

  • Remember that " " and "0" are truthy strings; only the empty string is falsy. [S1]

  • Do not use if (array) to test whether an array has items. Empty arrays are truthy. [S1]

  • Use a direct condition such as items.length > 0 when checking for array entries.

  • Choose an explicit comparison or content check when truthiness is broader than your intention.

A few more useful answers

Is a truthy value the same as `true`?

No. A truthy value is treated like true in a Boolean context, but it can still be a string, number, array, object, or another type. [S2]

Does `if (value)` check whether a variable exists?

It checks whether the value is truthy, not just whether it exists. For example, an existing value of 0 or "" is falsy, while an empty array is truthy. [S1]

What is the quickest way to check whether an array has items?

Use a condition such as items.length > 0. A plain if (items) does not distinguish an empty array from a nonempty one. [S1]

Sources

References used for this guide.

  1. [S1] Truthy — MDN
    developer.mozilla.org · Checked
  2. [S2] Boolean — JavaScript — MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work