string
coding
boolean
javascript

How can I convert a string to boolean in JavaScript?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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.

javascript
1console.log(Boolean("true"));
2console.log(Boolean("false"));
3console.log(Boolean("0"));
4console.log(Boolean(""));

Output:

text
1true
2true
3true
4false

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.

javascript
1function parseBoolean(value) {
2  if (typeof value !== "string") {
3    throw new TypeError("Expected a string");
4  }
5
6  const normalized = value.trim().toLowerCase();
7
8  if (normalized === "true") return true;
9  if (normalized === "false") return false;
10
11  throw new Error("Invalid boolean string");
12}
13
14console.log(parseBoolean("true"));
15console.log(parseBoolean(" FALSE "));

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.

javascript
1function parseLooseBoolean(value) {
2  if (typeof value !== "string") return null;
3
4  const normalized = value.trim().toLowerCase();
5
6  if (["true", "1", "yes", "on"].includes(normalized)) return true;
7  if (["false", "0", "no", "off"].includes(normalized)) return false;
8
9  return null;
10}
11
12console.log(parseLooseBoolean("yes"));
13console.log(parseLooseBoolean("0"));
14console.log(parseLooseBoolean("maybe"));

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:

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

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 true or false
  • 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 yes and no
  • Choose the conversion rule that matches the actual data format

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.