Matthew C.
—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?
You can use an equality operator, Boolean()
constructor, or the double NOT (!!
) operator to convert a “true” or “false” string to a boolean.
===
)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"
Boolean()
Constructor or the Double NOT (!!
) OperatorYou 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.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,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.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.