Sub topic 3
Everything Else Is Truthy, Including Some Surprises
This chapter assumes you know JavaScript's primitive types and objects, can write if statements and the ternary operator, and can read console.log output. It also builds on the previous chapter's list of eight falsy values.
The Rule: Not Falsy Means Truthy
JavaScript does not keep a list of truthy values. It keeps a short list of falsy values: false, 0, -0, 0n, "", null, undefined and NaN. Every other value is truthy. A truthy value is one that behaves like true when JavaScript needs a boolean, as in an if condition or a ternary test.
Because the rule works by exclusion, you never have to memorize truthy values. You only check whether a value is on the falsy list. If it is not, it is truthy.
Surprising Truthy Values
Most mistakes happen when a value looks empty or false but is not on the falsy list. The script below tests several of these values with a ternary, so no explicit conversion is needed.
function check(label, value) {
console.log(label.padEnd(20), value ? "truthy" : "falsy");
}
check('"0"', "0");
check('"false"', "false");
check('" " (one space)', " ");
check("[]", []);
check("{}", {});
check("-1", -1);
check("Infinity", Infinity);
check("function", function () {});
check("new Boolean(false)", new Boolean(false));
check("new Date('bad')", new Date("bad"));Every line prints truthy. Here is why each one surprises people.
| Value | Why it is truthy |
|---|---|
| `"0"`, `"false"` | They are non-empty strings. JavaScript does not read the text inside a string. |
| `" "` | A string holding only whitespace still has length 1. Only `""` is falsy. |
| `[]`, `{}` | All objects are truthy, even when they are empty. |
| `-1`, `Infinity` | Only `0`, `-0` and `NaN` are falsy numbers. |
| `new Boolean(false)` | This is a wrapper object, and every object is truthy. |
| `new Date("bad")` | An invalid date is still an object, even though its time value is `NaN`. |
Checking for Empty Arrays and Objects
Since [] and {} are truthy, if (items) only tells you that items exists. It does not tell you whether the array has anything in it. Test the contents directly:
const items = [];
const config = {};
if (items) console.log("items exists"); // logs
if (items.length > 0) console.log("has items"); // does not log
if (Object.keys(config).length > 0) {
console.log("config has keys"); // does not log
} else {
console.log("config is empty"); // logs
}For a string that might contain only spaces, use str.trim() !== "" instead of if (str).
Exercise
Which of these values are truthy: "0", 0n, [], " ", NaN, new Boolean(false), -0, {}?
Show answer
"0", [], " ", new Boolean(false) and {} are truthy. 0n, NaN and -0 are falsy because they are on the falsy list. The other values are non-empty strings or objects, so they are truthy.
Exercise
A function receives tags, which is always an array. The code if (tags) { render(tags); } calls render even when there are no tags. Fix the condition.
Show answer
An empty array is truthy, so the check always passes. Use if (tags.length > 0) { render(tags); } so that render runs only when the array has at least one element.