Sub topic 1
What Counts as True: Truthy and Falsy Values
Every if, while, and for condition in JavaScript accepts any value, not only true or false. Before the engine decides which way to go, it converts the value to a boolean. Values that convert to true are called truthy, and values that convert to false are called falsy. If you know the falsy list, you can predict how every condition in the rest of this tutorial behaves.
Prerequisites
This chapter assumes you can:
- Read basic JavaScript syntax: variables, expressions, and statements.
- Declare variables with
letandconst. - Tell apart primitive types, arrays, and plain objects.
- Write and call functions, including arrow functions.
The Falsy List
JavaScript has a short, fixed list of falsy values. Every other value is truthy.
| Value | Type | Notes |
|---|---|---|
| `false` | boolean | The literal itself |
| `0`, `-0` | number | Both zeros |
| `0n` | bigint | BigInt zero |
| `""` | string | Only the empty string |
| `null` | null | Intentional absence |
| `undefined` | undefined | Missing value |
| `NaN` | number | Result of failed math |
You can check any value with Boolean(value), which applies the same conversion a condition uses:
console.log(Boolean(0), Boolean(""), Boolean(null), Boolean(NaN));
// false false false false
console.log(Boolean([]), Boolean({}), Boolean("0"), Boolean("false"));
// true true true trueSurprising Truthy Values
Many common mistakes come from values that look empty but are truthy:
[]and{}: every object is truthy, however empty it is."0","false"," ": any string with at least one character, even a space, is truthy.new Boolean(false): a wrapper object, so it is truthy.Infinityand negative numbers: only zero is falsy.
To test whether an array or object is empty, check its size directly:
const items = [];
const config = {};
if (items) console.log("runs: [] is truthy");
if (items.length === 0) console.log("items is empty");
if (Object.keys(config).length === 0) console.log("config is empty");When a Truthiness Check Is Too Loose
A check like if (count) treats 0 the same as undefined. That is fine when zero really means "nothing to do." It is a bug when zero is a valid value:
function describeStock(count) {
if (!count) {
return "unknown";
}
return `${count} in stock`;
}
console.log(describeStock(5)); // "5 in stock"
console.log(describeStock(0)); // "unknown" (wrong: 0 is a real count)
console.log(describeStock(undefined)); // "unknown"The fix is to say which values you mean to exclude:
function describeStock(count) {
if (count === undefined || count === null) {
return "unknown";
}
return `${count} in stock`;
}
console.log(describeStock(0)); // "0 in stock"Ask yourself whether 0, "", or NaN could be legitimate values before you rely on plain truthiness. Later chapters show shorter ways to write this check with == and ??.
Double Negation
!value gives the opposite boolean, and !!value gives the boolean itself. It behaves exactly like Boolean(value). Use whichever reads better to your team when you need a real true or false, for example to store in a flag:
const user = { name: "Ana" };
const hasUser = !!user;
console.log(hasUser); // trueExercise
Which of these values are falsy: "0", 0n, [], -0, " ", NaN, {}, null?
Show answer
0n, -0, NaN, and null are falsy. "0" and " " are non-empty strings, and [] and {} are objects, so all four are truthy.
Exercise
A function starts with if (!name) return "Anonymous";. A user deliberately enters an empty string as their display name, and the product should keep it. What goes wrong, and how do you fix the condition?
Show answer
The empty string is falsy, so the function returns "Anonymous" and loses the user's choice. Check only for missing values instead: if (name === undefined || name === null) return "Anonymous";.