Readers usually land here for one of two different problems:
- Coercion — “What is the truthy/falsy boolean value of this string?” (
Boolean(str),!!str). - Semantic parsing — “The user typed
'true'or'false'; how do I get a real boolean?” (trim, case-insensitive compare, orJSON.parsefor JSON-shaped data).
Those are not the same. Coercion treats any non-empty string as true. Parsing treats only specific tokens as boolean.
Tested on: Node.js v22 on Linux; the same coercion and parsing rules apply in browsers. A short note after each snippet describes the expected values.
Coercion: Boolean() and !!
Boolean(value) converts a value using normal JavaScript boolean coercion. For strings, only the empty string is false.
console.log(Boolean("")); // false
console.log(Boolean("false")); // true — still a non-empty string!
console.log(Boolean("0")); // true
The !! idiom is shorthand for the same result:
const s = "hello";
console.log(!!s); // true
Use coercion when you only care whether a string is empty vs non-empty. For related reading, see JavaScript equality and double question mark (??).
Parse canonical "true" / "false" text
When you read query params, env vars, or CSV cells, normalize then compare:
function parseBooleanString(input) {
const v = String(input).trim().toLowerCase();
if (v === "true" || v === "1" || v === "yes") return true;
if (v === "false" || v === "0" || v === "no") return false;
return null; // or throw — unknown token
}
console.log(parseBooleanString(" TRUE "));
console.log(parseBooleanString("false"));
console.log(parseBooleanString("maybe"));You should see three lines: true for " TRUE ", false for "false", then null for the unknown token "maybe".
Adjust the accepted tokens ("on", "off", etc.) to match your API contract.
Parse JSON booleans
If the string is valid JSON (including quoted booleans), use JSON.parse:
console.log(JSON.parse("true"));
console.log(JSON.parse("false"));The first call prints boolean true, the second boolean false (JSON values, not strings).
Wrap in try/catch for untrusted input. For full detail see JavaScript JSON.parse.
Summary
Boolean(str)/!!str— answers “empty or not?”;"false"is still true.parseBooleanString-style helpers — answers “what did the user mean?”JSON.parse— for actual JSON fragments, not arbitrary free-form text.
