Boolean Conversion: Why "false" Can Be True in Code
Here is a bug that catches almost every beginner: Boolean("false") in JavaScript returns true. It feels wrong, but the rule is simple. Any text that is not empty counts as true, even if the word inside is "false".
Truthy and falsy
In JavaScript, some values act as false when used in a condition. They are called falsy. There are only a few: false, 0, -0, 0n, an empty string, null, undefined and NaN. Everything else is truthy. That includes the text "0", the text "false", an empty array [] and an empty object {}.
Python is similar, with one difference
In Python, bool("False") is also True, because the text is not empty. But an empty list and an empty dictionary are False, unlike JavaScript.
Where this hurts
Settings are often saved as text: "true", "false", "1", "0", "yes", "no". If you load one and check it directly, "false" will turn the feature on. This is a classic cause of wrong behaviour in config files, environment variables and form data.
The safe way
Compare the text to the words you accept:
function toBool(text) {
return ["true", "yes", "1", "on"].includes(text.trim().toLowerCase());
}
Decide what happens with unknown words. Treating them as false is common, but sometimes it is better to show an error.
Do not use == with booleans
Loose comparison gives odd results: "0" == false is true, but "0" on its own is truthy. Use === to avoid surprises.
See it in action
The Boolean Conversion tool on our Developer Converters page lets you type several values, one per line, and shows how JavaScript, Python and a word-based check treat each one. It is a quick way to learn the rules and test your own inputs.
Frequently asked questions
Why is the string "false" true in JavaScript?
Any non-empty text is truthy, whatever it says. The text "false" has five characters, so Boolean("false") is true. Compare the text instead: value === "true".
How do I read yes/no values from a form or CSV?
Lower-case and trim the text, then check it against lists of known words, such as yes, y, true, 1 and on for true, and no, n, false, 0 and off for false. Treat anything else as an error rather than guessing.
Is 0 false in every language?
In JavaScript, Python, C and PHP, the number 0 is false. In Ruby, only nil and false are false, so 0 is true. Always check the rules of the language you are using.
What about empty arrays and objects?
In JavaScript an empty array [] and an empty object {} are both truthy. In Python an empty list, dictionary or string is falsy. This difference catches people who switch between the two languages.
Open the free Developer Converters. It runs in your browser, so nothing is uploaded.