Sub topic 14
Truthy Is Not the Same as == true
This chapter assumes you know the falsy values and know that if converts its condition with the same rules as Boolean(). Here we compare that conversion with the loose equality operator ==. Many developers assume the two follow the same rules. They do not.
Two Different Conversion Rules
Truthiness has a single rule: a value is falsy if it appears on the falsy list, and every other value is truthy. if (x), !x, && and || all use this rule.
Loose equality uses a separate algorithm. When one side of == is a boolean, JavaScript converts that boolean to a number first (false becomes 0 and true becomes 1). It then compares again, converting the other side toward a number as well. Objects are converted to primitives first, which usually means turning them into strings.
console.log([] == false); // true
console.log(Boolean([])); // true
console.log("0" == false); // true
if ("0") console.log("\"0\" is truthy"); // logsThe empty array [] is truthy, yet [] == false is true. The comparison goes through these steps: false becomes 0, [] becomes "", and "" becomes 0. 0 == 0 is true. Truthiness was never checked.
Comparing Results Side by Side
The table shows where the two rules disagree.
| Value `x` | `Boolean(x)` | `x == false` | `x == true` |
|---|---|---|---|
| `"0"` | true | true | false |
| `""` | false | true | false |
| `" "` | true | true | false |
| `[]` | true | true | false |
| `[0]` | true | true | false |
| `[1]` | true | false | true |
| `"2"` | true | false | false |
| `null` | false | false | false |
| `undefined` | false | false | false |
| `NaN` | false | false | false |
Two patterns are worth remembering:
"2" == trueisfalseeven though"2"is truthy, becausetruebecomes1and2 !== 1. Any value whose numeric form is not exactly1fails== true.null,undefinedandNaNare falsy, but none of them is== false.nullandundefinedare loosely equal only to each other, andNaNis equal to nothing, including itself.
You can check every row yourself:
const samples = ["0", "", " ", [], [0], [1], "2", null, undefined, NaN];
for (const x of samples) {
console.log(
JSON.stringify(x) ?? String(x),
Boolean(x),
x == false,
x == true
);
}JSON.stringify(undefined) returns undefined, so the code uses ?? to fall back to String(x) for that row. NaN prints as null because JSON has no representation for it.
What to Write Instead
Pick the form that says what you mean:
- To test truthiness, write
if (x)orif (!x). - To test whether a value is the boolean
true, writex === true. Strict equality never converts types. - To test for a missing value, write
x == null(which matches bothnullandundefined) orx === undefined.
const flag = "false"; // a string, for example from a query parameter
console.log(Boolean(flag)); // true: non-empty string
console.log(flag === true); // false: not the boolean true
console.log(flag === "true"); // false: compare the string explicitlyStrings like "false" and "0" from forms, URLs or environment variables are a common source of bugs. Compare them as strings, or parse them on purpose. Do not rely on truthiness or == to read their meaning.
Exercise
Predict the output of each line: console.log("1" == true);, console.log(" " == false);, console.log(undefined == false);, and if (" ") console.log("runs");.
Show answer
"1" == true is true (true becomes 1, "1" becomes 1). " " == false is true (false becomes 0, a whitespace-only string becomes 0). undefined == false is false (undefined is loosely equal only to null and undefined). The if logs "runs" because " " is a non-empty string and therefore truthy.
Exercise
A teammate writes if (items == false) showEmptyState(); where items is an array. Explain when it works, when it fails, and give a correct replacement.
Show answer
It shows the empty state for [] because [] converts to "" and then to 0. It also shows it for [0] and [""], which are not empty. Use if (items.length === 0) showEmptyState(); instead.