How can I convert a string to boolean in JavaScript?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Converting a string to a boolean in JavaScript sounds simple, but there are actually several different behaviors you might want. The right solution depends on whether you want JavaScript truthiness, strict parsing of the words true and false, or a looser parser for values such as yes, no, 1, and 0.
Truthiness Is Not Boolean Parsing
The first trap is Boolean(value) or !!value. Those do not parse the text content of the string. They only ask whether the string is empty.
Output:
That behavior is correct for truthiness, but it is the wrong tool if you expect the string "false" to become the boolean false.
Strict Parsing of true and false
If the input should only contain the literal words true or false, compare a normalized string directly.
This is usually the safest option for APIs, config values, and query parameters where the accepted input format should be narrow and predictable.
Loose Parsing for User-Friendly Input
Sometimes you want to accept a wider vocabulary. For example, a command-line tool or admin form might accept yes, no, 1, 0, on, and off.
Returning null or throwing an error for unknown values is usually better than silently guessing.
JSON.parse Works Only in Narrow Cases
If the string is guaranteed to be valid JSON and contains only true or false, JSON.parse works:
But it has limits:
- it throws on invalid JSON
- it can parse non-boolean values too
- it is heavier than a direct string comparison
For example, JSON.parse("0") returns the number 0, not a boolean.
So JSON.parse is acceptable when the input is already JSON, but it is not the general-purpose string-to-boolean solution most people want.
Pick the Behavior That Matches the Data
A useful decision rule is this:
- use
Boolean(value)only if you care about empty versus non-empty strings - use strict string comparison when the input should be
trueorfalse - use a custom parser if you intentionally support several human-friendly spellings
Those are different problems, and trying to force them into one “magic conversion” usually creates bugs.
If the value comes from HTML forms, URL query strings, or environment variables, decide the accepted vocabulary once and centralize the parsing in one helper. That prevents one part of the codebase from treating 0 as false while another part silently treats it as true because it is a non-empty string.
Common Pitfalls
The biggest mistake is using Boolean("false") and expecting false. Any non-empty string is truthy in JavaScript.
Another mistake is using JSON.parse on unchecked user input without error handling. Invalid JSON throws immediately.
A third issue is supporting many alternate spellings without documenting them. If your app accepts yes, on, and 1 as true, that should be deliberate and consistent across the whole codebase.
Summary
- '
Boolean(str)checks truthiness, not semantic boolean meaning' - The string
"false"is truthy because it is non-empty - For exact parsing, compare normalized strings such as
"true"and"false" - Use a custom helper if you need looser input such as
yesandno - Choose the conversion rule that matches the actual data format

