How to Convert a String to Boolean in TypeScript

To convert a string to boolean in TypeScript, compare the text: value.trim().toLowerCase() === "true". Don’t use Boolean(value) or !!value: they test whether the string is empty, so Boolean("false") is true:

const isActive = value.trim().toLowerCase() === "true";

Truthiness rules are easy to misremember, so I let TypeScript 7.0.2 and Node v22.22.2 settle each case; the env-var demo sets DEBUG on the same Command Prompt line.

You’ll see the Boolean() trap, strict comparison, a parser for yes/no/1/0 that returns undefined for junk, JSON.parse, and parsing environment variables and URL parameters.

Why does Boolean(“false”) return true?

In JavaScript, every non-empty string is truthy. Boolean() and !! only check for an empty string, not for the word inside it:

const inputs = ["true", "false", "0", "", " ", "no"];

for (const s of inputs) {
  console.log(JSON.stringify(s).padEnd(8), "Boolean():", String(Boolean(s)).padEnd(6), "!!s:", !!s);
}

Output:

"true"   Boolean(): true   !!s: true
"false"  Boolean(): true   !!s: true
"0"      Boolean(): true   !!s: true
""       Boolean(): false  !!s: false
" "      Boolean(): true   !!s: true
"no"     Boolean(): true   !!s: true
Command Prompt output of TypeScript Boolean() and double negation returning true for the strings false, 0, a space and no
“false”, “0”, a single space and “no” all come out true; only the empty string gives false.

That’s the bug behind most broken feature flags. The types themselves are explained in boolean vs Boolean in TypeScript.

Convert a string to boolean with a strict comparison

Compare against the word you expect. Normalize first, because form fields and config files often carry capitals or stray spaces:

const fromForm: string = "TRUE ";             // typed as string, like real form input

console.log(fromForm === "true");                          // false: case and space differ
console.log(fromForm.trim().toLowerCase() === "true");     // true

function toBool(value: string): boolean {
  return value.trim().toLowerCase() === "true";
}
console.log(toBool("True"), toBool("false"), toBool("yes"));   // "yes" isn't "true"

Output:

false
true
true false false

This treats anything other than “true” as false, including “yes”. That’s fine for checkboxes, but not for user-typed settings. Trimming is covered in removing spaces from a string in TypeScript.

Parse yes/no, 1/0 and on/off into a boolean

For real input, accept the common spellings and return undefined for anything else, so a typo doesn’t silently become false:

const TRUE_WORDS = new Set(["true", "yes", "y", "1", "on"]);
const FALSE_WORDS = new Set(["false", "no", "n", "0", "off", ""]);

function parseBool(value: string | null | undefined): boolean | undefined {
  if (value == null) return undefined;
  const v = value.trim().toLowerCase();
  if (TRUE_WORDS.has(v)) return true;
  if (FALSE_WORDS.has(v)) return false;
  return undefined;                              // unknown -> let the caller decide
}

for (const s of ["Yes", " no ", "1", "OFF", "", "maybe", null]) {
  console.log(JSON.stringify(s).padEnd(8), "->", parseBool(s));
}

const optIn = parseBool("maybe") ?? false;       // pick a default explicitly
console.log("newsletter opt-in:", optIn);

Output:

"Yes"    -> true
" no "   -> false
"1"      -> true
"OFF"    -> false
""       -> false
"maybe"  -> undefined
null     -> undefined
newsletter opt-in: false
Command Prompt output of a TypeScript parseBool function mapping yes, no, 1, OFF and empty strings to booleans and maybe to undefined
Yes and 1 give true, no, OFF and the empty string give false, and maybe or null give undefined until a default is chosen.

The boolean | undefined return type forces callers to pick a default with ??. Checking for blank input first is shown in checking if a string is empty in TypeScript.

Convert a string to boolean with JSON.parse()

JSON.parse("true") works, but only for exact lowercase JSON. Anything else either throws or returns a different type:

for (const s of ["true", "false", "True", "1"]) {
  try {
    const v: unknown = JSON.parse(s);
    console.log(JSON.stringify(s).padEnd(7), "->", v, typeof v);
  } catch (err) {
    console.log(JSON.stringify(s).padEnd(7), "->", (err as Error).name);
  }
}

Output:

"true"  -> true boolean
"false" -> false boolean
"True"  -> SyntaxError
"1"     -> 1 number
Command Prompt output of TypeScript JSON.parse converting true and false, throwing SyntaxError for True and returning a number for 1
“true” and “false” become booleans, “True” throws SyntaxError, and “1” comes back as the number 1.

Because it returns any, type it as unknown and check. For full JSON documents, see converting JSON to an array in TypeScript.

Read boolean environment variables and URL parameters

Env vars and query strings are always strings or missing. Run them through the parser and choose a default per setting:

function parseBool(value: string | null | undefined): boolean | undefined {
  if (value == null) return undefined;
  const v = value.trim().toLowerCase();
  if (["true", "yes", "1", "on"].includes(v)) return true;
  if (["false", "no", "0", "off", ""].includes(v)) return false;
  return undefined;
}

declare const process: { env: Record<string, string | undefined> };
const debug = parseBool(process.env.DEBUG) ?? false;
const sendEmails = parseBool(process.env.SEND_EMAILS) ?? true;
console.log("DEBUG:", process.env.DEBUG, "->", debug);
console.log("SEND_EMAILS:", process.env.SEND_EMAILS, "->", sendEmails);

const url = new URL("https://shop.example.com/products?inStock=false&onSale=1&city=Austin");
const inStock = parseBool(url.searchParams.get("inStock"));
const onSale = parseBool(url.searchParams.get("onSale"));
const featured = parseBool(url.searchParams.get("featured"));    // missing -> null -> undefined
console.log({ inStock, onSale, featured });

Output:

DEBUG: false -> false
SEND_EMAILS: undefined -> true
{ inStock: false, onSale: true, featured: undefined }
Command Prompt output of TypeScript reading a DEBUG environment variable and URL search params as booleans with defaults
DEBUG=false becomes false, the unset SEND_EMAILS falls back to true, and the URL gives inStock false, onSale true and featured undefined.

searchParams.get() returns null for a missing key, which the parser maps to undefined. Going back the other way is covered in converting a boolean to a string in TypeScript.

String to boolean methods compared

Method“false”“TRUE ““maybe”
Boolean(s) / !!strue ✗truetrue ✗
s === "true"falsefalse ✗false
s.trim().toLowerCase() === "true"falsetruefalse
JSON.parse(s)falseSyntaxErrorSyntaxError
parseBool(s)falsetrueundefined

More TypeScript conversions you might need:

Frequently asked questions

How do I convert a string to boolean in TypeScript?

Use value.trim().toLowerCase() === "true", or a parser that also accepts yes/no and 1/0.

Why is Boolean(“false”) true?

Any non-empty string is truthy. Boolean() only checks emptiness, not the text.

Can I use JSON.parse to convert “true” to true?

Yes, for exact lowercase “true” and “false”. Other input throws or returns another type.

How do I convert “yes” or “1” to a boolean?

Map known words with a Set lookup after trimming and lowercasing, and return undefined for anything else.

How do I read a boolean environment variable?

Parse process.env.NAME with your parser and add a default: parseBool(process.env.DEBUG) ?? false.

Where is Boolean documented?

See Boolean on MDN.