What is a boolean — and why the string "false" is actually true

Here's a puzzle. A user turned off notifications in settings. The server sent "notifications": "false". The code checks if (notifications) — and… sends a notification.
No magic. The word "false" in quotes isn't false. It's a five-letter string. And to JavaScript, any non-empty string is true. Let's see how "yes" and "no" work in code, and where they lie.
What a boolean is
A boolean (named after the mathematician George Boole) is the simplest data type. It has only two options: true and false. Like a light switch: the light is either on or off. There's no third state.
const isLoggedIn = true
const hasPaid = false
const isAdult = age >= 18 // a comparison produces true or false by itself
Booleans come from questions. "Is the age over 18?" — true or false. "Do the passwords match?" — same. Every comparison (>, <, ===) answers this way.
Names of such variables usually start with is, has, can: isOpen, hasError, canEdit. See one in AI-written code — it almost certainly holds true or false.
Why a program needs "yes" and "no"
Nearly every decision in an app is a fork. Show "Log out" or "Log in"? Let the user into the payment page? Highlight a field in red? Behind each choice is one check that boils down to true or false.
if (isLoggedIn) {
showProfile()
} else {
showLoginButton()
}
Booleans combine into bigger conditions:
&&— "and":isLoggedIn && hasPaid— true only if both are true;||— "or":isAdmin || isOwner— one is enough;!— "not":!isLoadingflips the value.
That's the whole kit. The logic of any screen is built from these three parts.
Truthy and falsy: where JavaScript cheats
Now the part the definitions skip. Inside an if, JavaScript is willing to check anything, not just true and false. It silently turns any value into a "yes" or a "no".
Only a short list counts as false (falsy):
false0""— an empty stringnullandundefinedNaN
Everything else is true (truthy). And that's where the surprises start:
Boolean("false") // true — a non-empty string
Boolean("0") // true — also a non-empty string
Boolean([]) // true — an empty array!
Boolean({}) // true — an empty object
Boolean(0) // false
Each line is a real-life bug:
"false"from a server or localStorage. Everything there is stored as strings. You savedfalse— you got back"false", and it's truthy.- An empty array.
if (items)passes even for[]. The "nothing found" screen never shows. The right check isif (items.length > 0). - Zero. A price of
0— a free item — suddenly counts as "no price", and the price block disappears.
How to check properly
The mental model is simple: ask the code exactly what you mean.
- Want to know if there are items — ask for the length:
items.length > 0. - Want to know if a value arrived, and zero is allowed — check for emptiness explicitly:
price !== null && price !== undefined, orprice != null. - A flag arrived as a string — compare with a string:
value === "true". Or better, ask the server to send a realtrue.
In a prompt for AI, one sentence helps: "store flags as booleans, not strings; zero is a valid value." The model stops writing the lazy if (price).
If you use TypeScript, it catches some of these before the code even runs. More in the TypeScript vs JavaScript comparison.
Where you'll meet booleans
Literally everywhere there's an on/off:
- checkboxes and toggles in forms — their value is
trueorfalse; - screen state:
isLoading,isOpen,hasError; - permissions:
isAdmin,canEdit; - database columns like
is_paidoris_deleted.
What's the difference between == and === when checking for true?
Double equals tries to convert types and gets confused: [] == false is true. Triple equals compares honestly, with no conversions. Use === almost always.
What does "!!" mean in code?
Double negation turns any value into an honest boolean. !!"text" gives true, !!0 gives false. It's shorthand for Boolean(value).
Does Python work the same way?
Similar, but an empty list is falsy there. if []: won't run in Python, while in JavaScript it will. Porting code between languages — check those spots first.
Short story-lessons, an agent simulator and daily practice — in our mobile app. Free.





