false in JavaScript: the false value and the eight falsy values

false is the negative boolean value in JavaScript. Here are the eight values treated as false, and the two of them that cause the most damage.
3 min read
Believemy logo

Plenty of bugs come down to one simple confusion: believing that whatever fails a test equals false. Seven other values fail tests without being that boolean, and two of them, zero and the empty string, are perfectly valid data that gets wiped out by accident.


Definition

false is a literal of the language, the negative boolean value, the other one being true. It is written in lowercase, without quotes, and it is a reserved word.

You get it either by writing it, or, far more often, as the result of a comparison that does not hold.

JAVASCRIPT
const isPaid = false;

console.log(3 > 10);             // false
console.log("a" === "b");        // false
console.log([1, 2].includes(9)); // false

console.log(typeof false);       // "boolean"

One detail worth noting in passing: on an empty array, [].some(...) always returns false and [].every(...) always returns true, whatever function you hand them.


The eight values treated as false

A test does not require a boolean: it converts whatever it is given. The values that turn false during that conversion are called falsy, and there are few enough of them to learn by heart.

ValueWhat it stands for
falseThe boolean itself
0 and -0The numeric zeros
0nZero as a big integer
""The empty string
nullEmptiness on purpose
undefinedEmptiness by default
NaNA numeric calculation that failed

Everything else is true, without exception. An empty array, an empty object, the string "0" and the string "false" all pass a test, which contradicts intuition rather violently. The full subject is covered in Truthy and falsy.


The zero and empty string trap

This is the costliest defect on the list, because nothing breaks as long as the tested values stay large.

JAVASCRIPT
function showStock(quantity) {
  // Trap: 0 is a valid quantity, yet falsy
  return quantity || "not provided";
}

console.log(showStock(12)); // 12
console.log(showStock(0));  // "not provided", while the stock really is 0!

// Fix: only replace null and undefined
function showStockFixed(quantity) {
  return quantity ?? "not provided";
}

console.log(showStockFixed(0)); // 0

The same problem hits a zero percent discount, a score of zero, a billing address deliberately left blank. The practical rule: whenever a fallback concerns numeric or textual data, prefer Nullish coalescing (??) over ||.

Good to know

A falsy value is not equal to false. Both fail a test, yet 0 === false is false, and so is "" === false. They behave alike, they are not identical.


Frequently asked questions

Question

Why is an empty array not falsy?

Because an array is an object, and no object is falsy in JavaScript, whatever it contains. The list of eight false values holds primitives only. To test an array, go through its size with Array.isArray(a) && a.length > 0, which asks the real question.


Question

What is the difference between false and null in an API response?

A considerable difference in meaning. false states a negative answer: the order is not paid. null states that there is no information: whether it is paid remains unknown. Conflating the two turns an unknown into a no, which distorts every decision made afterwards.


Question

How do I get false from another value?

By calling Boolean(value), which applies exactly the rules in the table above, or by writing !!value, which produces the same result in two characters. This is useful before storing an answer in a database, where a real boolean is wanted rather than an empty string.

Related terms

Discover our javaScript glossary

Every word of JavaScript explained simply: keywords, built-in objects, methods, errors and concepts. Clear definitions and examples that actually run, to learn and to troubleshoot.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.