Introduction
Suppose we have boolean values stored in our database as strings and based on those values we want to perform some specific operation on our website/application. In that case, we have to convert those strings into boolean values before using them in logical operations.
In this article, we'll take a look at several ways of converting string values to boolean (
true
orfalse
) in JavaScript.
Using Identity Operator (===)
The identity operator, also known as a strict equality operator, returns true
only if and only if both values being compared are of the same type and have the same value. In other words, it determines whether the value on the left-hand side is equal to the value on the right-hand side - and returns true
if they are, and false
if they are not.
Note: If you want to learn more about the difference between ==
(strict equality operator) and ===
(loose equality operator), you should read our "JavaScript: == vs === Operator"!
Essentially, we'll compare our string to the string "true"
. Therefore the output will be a boolean true
only if our string is actually "true"
. Any other string will cause the code to return the false
boolean value:
let myString = "true";
let boolOutput = (myString === "true"); //returns true
Note: We write a string value with quotes - "true"
, and the boolean value without quotes - true
. We'll use this notation throughout this whole article.
Additionally, we can convert a string to lowercase first, just to make sure the letter case won't cause any faulty outputs:
let myString = "True";
let boolOutput = (myString.toLowerCase() === "true"); // returns true
As we've stated before, the previous code will return false
if our string value is not equal to "true"
:
let myString1 = "Test";
let boolOutput1 = (myString1 === "true"); //returns false
let myString1 = "Test";
let boolOutput1 = (myString1.toLowerCase() === "true"); //returns false
let myString = "True";
let boolOutput2 = (myString2 === "true"); //returns false
We can also spice things up a little by introducing the ternary operator alongside the equality operator. All we will do is check if our string is equal to "true"
and then return either a boolean value of true
if there is a match or false
if it doesn't:
let myString = "true";
let boolOutput = myString.toLowerCase() == 'true' ? true : false; // returns true
Using Regular Expressions (RegEx)
Regular expressions (RegEx) are patterns for matching and testing string character combinations.
Note: In this article, we'll assume you have at least a basic understanding of regular expressions in general. But if you need help to get a grasp on regular expressions in JavaScript, you should consider reading our "Guide to Regular Expressions and Matching Strings in JavaScript"
For the purpose of this article, we'll use the most basic form of regular expressions in JavaScript - we'll create the simple regex that matches "true"
and match it against our string using the test()
method:
let stringValue = "true";
let boolValue = (/true/).test(stringValue); //returns true
You will notice this is actually case-sensitive, as this will return false
if it has slight case inconsistency:
let stringValue = "True";
let boolValue = (/true/).test(stringValue); //returns false
To fix this, we can add /i
at the end of the regular expression to ensure for case-insensitive match:
let stringValue = "True";
let boolValue = (/true/i).test(stringValue); //returns true
Using the Boolean Wrapper Class?
JavaScript has a built-in Boolean
object for storing boolean values. It is actually an object wrapper for boolean values - it wraps around other objects thus making them a valid boolean value. This is done by testing the truthy-falsy value of an object. In general - empty objects are evaluated to false
, and non-empty objects are evaluated to true
.
Any string which isn't the empty string will evaluate to true
by using the Boolean
wrapper:
let myString1 = Boolean('true'); //returns true
let myString2 = Boolean(''); // //returns false
let myString3 = Boolean('false'); //returns true
let myString4 = Boolean('True'); //returns true
There are two major issues here:
- The first is that this will return
true
for an empty string with at least one blank character (space, tab, etc.), that is why we have to be cautious when using this method:
const myString5 = Boolean(' '); //returns true
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
- Secondly, converting a string of
"false"
to a boolean value offalse
will fail because any non-empty string converts totrue
.
Double NOT Operator - !!
Using the double NOT operator is equal to using the logical NOT operator (!
) twice, which means that it inverts the result of the single NOT operator:
let myString1 = !'test'; // returns false
let myString2 = !''; // returns true
When we use the double NOT operator, the values are flipped, meaning we are now performing a pure boolean conversion:
let myString1 = !!'test'; // returns true
let myString2 = !!''; // returns false
The double NOT (!!
) operator is quite concise but does the same thing as the Boolean
wrapper. However, it's a bit harder to read if you're not familiar with the logical NOT (!
) operator.
We also have to be cautious when using this method as an empty string with at least one blank character will still return true
and when we try to convert a string of "false"
to a boolean value of false
, this will still not work (just as with Boolean
object).
Conclusion
In this article, we've taken a look at four ways to convert a string into a boolean in JavaScript. The simplest way to do so is to use the strict equality operator to compare our string value to the "true"
- if the string is (strictly) equal to "true"
, the output will be boolean true
. Alternatively, you can use the ternary operator alongside the loose equality operator to achieve the same. Also, regular expression matching is a solid approach.
The last two methods, Boolean
object and double NOT operator, have a simpler syntax, but their drawback is the way they treat the false
value - string "false" will return the boolean value true
, which makes them applicable only to a small subset of conversion cases.