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”:

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:

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”:

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:

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:

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:

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:

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.

Join the discussionCome work with us
Share on Twitter
Bookmark this page
Ask a questionImprove this Answer

Related Answers

A better experience for your users. An easier life for your developers.

    TwitterGitHubDribbbleLinkedin
© 2023 • Sentry is a registered Trademark
of Functional Software, Inc.