Skip to content
  • Truthy & Falsy Values · JavaScript

Truthy & Falsy Values

LearnViaNotes @learnvianotes· 1 min read

In a boolean context every value is either truthy or falsy — and there are only eight falsy values to memorise.

The complete falsy list

false · 0 · -0 · 0n · "" (empty string) · null · undefined · NaN

Everything else is truthy — including "0", "false", " ", [], {}, function(){}, Infinity and new Boolean(false).

JavaScript
import { inspect } from "node:util";
const values = [0, -0, "", null, undefined, NaN, 0n, "0", "false", " ", [], {}, -1];
for (const v of values) {
  console.log(inspect(v).padEnd(9), "→", Boolean(v));
}
Output
0         → false
-0        → false
''        → false
null      → false
undefined → false
NaN       → false
0n        → false
'0'       → true
'false'   → true
' '       → true
[]        → true
{}        → true
-1        → true

Using truthiness

JavaScript
const items = [];
if (items.length) console.log("has items");
else console.log("empty array (length 0 is falsy)");

const name = "";
console.log(name || "Anonymous");
console.log(!!"text", !!0);
Output
empty array (length 0 is falsy)
Anonymous
true false

|| vs ?? for defaults

JavaScript
const settings = { volume: 0, title: "" };
console.log(settings.volume || 50, settings.volume ?? 50);
console.log(settings.title || "Untitled", settings.title ?? "Untitled");
console.log(settings.missing ?? "default");
Output
50 0
Untitled
default

List the falsy values in JavaScript.

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report