Parse a string to a boolean in JavaScript

Coerce strings with Boolean() and !!, or parse canonical values like 'true'/'false'. Learn the difference and avoid common mistakes.

Published

Updated

Read time 2 min read

Reviewed byDeepak Prasad

Parse a string to a boolean in JavaScript

Readers usually land here for one of two different problems:

  1. Coercion — “What is the truthy/falsy boolean value of this string?” (Boolean(str), !!str).
  2. Semantic parsing — “The user typed 'true' or 'false'; how do I get a real boolean?” (trim, case-insensitive compare, or JSON.parse for 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.

javascript
console.log(Boolean("")); // false
console.log(Boolean("false")); // true — still a non-empty string!
console.log(Boolean("0")); // true
Output

The !! idiom is shorthand for the same result:

javascript
const s = "hello";
console.log(!!s); // true
Output

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:

javascript
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"));
Output

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:

javascript
console.log(JSON.parse("true"));
console.log(JSON.parse("false"));
Output

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.

References

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …