Sentry Answers>JavaScript>

How can I convert a string to a boolean in JavaScript?

How can I convert a string to a boolean in JavaScript?

Matthew C.

The Problem

You have a string that you want to convert to a boolean. You may have a boolean value that gets converted to a string, for example when values are submitted via a form. How do you convert a “true” or “false” string to a boolean?

The Solution

You can use an equality operator, Boolean() constructor, or the double NOT (!!) operator to convert a “true” or “false” string to a boolean.

Using the Strict Equality Operator (===)

You can parse the string to a boolean using the strict equality operator(===). You can check if the value is equal to “true”:

Click to Copy
let trueStr = "true"; trueStr = (trueStr === "true"); console.log(trueStr, typeof trueStr); // true "boolean"

This would also work using the equality operator (==), which will try to coerce the values being compared to the same type before comparing them. It’s best to use the strict equality operator (===), as the type conversion of the equality operator (==) can lead to unexpected results:

Click to Copy
console.log("" === "0"); // false console.log(0 == ""); // true console.log(0 == "0"); // true console.log(false == "false"); // false console.log(false == "0"); // true console.log(false == ""); // true

You can also use the toLowerCase() method to do a case-insensitive check for “true”:

Click to Copy
let trueStr = "true"; trueStr = (trueStr.toLowerCase() === "true"); console.log(trueStr, typeof trueStr); // true "boolean"

The problem with the above code is that it will throw an error if the trueStr variable is null or undefined:

Click to Copy
Cannot read properties of undefined (reading 'toLowerCase')

There are two fixes for this. You can use the typeOf operator to check that the value is a string before lower casing the value:

Click to Copy
let trueStr = null; trueStr = (typeof trueStr === "string" && trueStr.toLowerCase() === "true"); console.log(trueStr, typeof trueStr); // false "boolean"

You can also use an if statement to first check if the trueStr variable is defined:

Click to Copy
let trueStr = null; if (trueStr) { trueStr = (trueStr.toLowerCase() === "true"); } console.log(trueStr, typeof trueStr); // null "object"

Using the Boolean() Constructor or the Double NOT (!!) Operator

You can use the Boolean() constructor to create a boolean object and then use its valueOf property to get the primitive boolean value. It takes in a value to convert to a boolean. If the value is falsy it will return false. If the value is truthy, it will return true. All values are truthy except: false, 0, -0, 0n, "", null, undefined, and NaN.

The double NOT (!!) operator coerces a value to a primitive boolean value, like the Boolean() constructor:

Click to Copy
const str1 = "true"; const str2 = "false"; const str3 = ""; const str4 = " "; console.log(!!str1); // true console.log(!!str2); // true console.log(!!str3); // false console.log(!!str4); // true console.log(Boolean(str1).valueOf()); // true console.log(Boolean(str2).valueOf()); // true console.log(Boolean(str3).valueOf()); // false console.log(Boolean(str4).valueOf()); // true

These methods are not ideal as “false” returns true because the only falsy string is an empty string.

  • YoutubeHow Sentry.io saved me from disaster (opens in a new tab)
  • ResourcesImprove Web Browser Performance - Find the JavaScript code causing slowdowns (opens in a new tab)
  • SentryJavascript Error Monitoring & Tracing (opens in a new tab)
  • ResourcesJavaScript Frontend Error Monitoring 101 (opens in a new tab)
  • Syntax.fmListen to the Syntax Podcast (opens in a new tab)
  • Syntax.fm logo
    Listen to the Syntax Podcast (opens in a new tab)

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “not bad” by 4 million developers and more than 150,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.