Introduction
Suppose now we have boolean values saved in our database as strings and based mostly on these values we wish to carry out some particular operation on our web site/utility. In that case, now we have to transform these strings into boolean values earlier than utilizing them in logical operations.
On this article, we’ll check out a number of methods of changing string values to boolean (
true
orfalse
) in JavaScript.
Utilizing Identification Operator (===)
The identification operator, also referred to as a strict equality operator, returns true
provided that and provided that each values being in contrast are of the identical kind and have the identical worth. In different phrases, it determines whether or not the worth on the left-hand aspect is the same as the worth on the right-hand aspect – and returns true
if they’re, and false
if they aren’t.
Notice: If you wish to study extra in regards to the distinction between ==
(strict equality operator) and ===
(unfastened equality operator), you must learn our “JavaScript: == vs === Operator”!
Primarily, we’ll examine our string to the string "true"
. Due to this fact the output will likely be a boolean true
provided that our string is definitely "true"
. Another string will trigger the code to return the false
boolean worth:
let myString = "true";
let boolOutput = (myString === "true");
Notice: We write a string worth with quotes – "true"
, and the boolean worth with out quotes – true
. We’ll use this notation all through this entire article.
Moreover, we will convert a string to lowercase first, simply to verify the letter case will not trigger any defective outputs:
let myString = "True";
let boolOutput = (myString.toLowerCase() === "true");
As we have said earlier than, the earlier code will return false
if our string worth just isn’t equal to "true"
:
let myString1 = "Take a look at";
let boolOutput1 = (myString1 === "true");
let myString1 = "Take a look at";
let boolOutput1 = (myString1.toLowerCase() === "true");
let myString = "True";
let boolOutput2 = (myString2 === "true");
We will additionally spice issues up a bit by introducing the ternary operator alongside the equality operator. All we are going to do is examine if our string is the same as "true"
after which return both a boolean worth of true
if there’s a match or false
if it does not:
let myString = "true";
let boolOutput = myString.toLowerCase() == 'true' ? true : false;
Utilizing Common Expressions (RegEx)
Common expressions (RegEx) are patterns for matching and testing string character mixtures.
For the aim of this text, we’ll use probably the most primary type of common expressions in JavaScript – we’ll create the easy regex that matches "true"
and match it towards our string utilizing the take a look at()
technique:
let stringValue = "true";
let boolValue = (/true/).take a look at(stringValue);
You’ll discover that is truly case-sensitive, as it will return false
if it has slight case inconsistency:
let stringValue = "True";
let boolValue = (/true/).take a look at(stringValue);
To repair this, we will add /i
on the finish of the common expression to make sure for case-insensitive match:
let stringValue = "True";
let boolValue = (/true/i).take a look at(stringValue);
Utilizing the Boolean Wrapper Class?
JavaScript has a built-in Boolean
object for storing boolean values. It’s truly an object wrapper for boolean values – it wraps round different objects thus making them a legitimate boolean worth. That is carried out by testing the truthy-falsy worth of an object. Basically – empty objects are evaluated to false
, and non-empty objects are evaluated to true
.
Any string which is not the empty string will consider to true
by utilizing the Boolean
wrapper:
let myString1 = Boolean('true');
let myString2 = Boolean('');
let myString3 = Boolean('false');
let myString4 = Boolean('True');
There are two main points right here:
- The primary is that it will return
true
for an empty string with at the least one clean character (house, tab, and so forth.), that’s the reason now we have to be cautious when utilizing this technique:
const myString5 = Boolean(' ');
Try our hands-on, sensible information to studying Git, with best-practices, industry-accepted requirements, and included cheat sheet. Cease Googling Git instructions and really study it!
- Secondly, changing a string of
"false"
to a boolean worth offalse
will fail as a result of any non-empty string converts totrue
.
Double NOT Operator – !!
Utilizing the double NOT operator is the same as utilizing the logical NOT operator (!
) twice, which signifies that it inverts the results of the only NOT operator:
let myString1 = !'take a look at';
let myString2 = !'';
After we use the double NOT operator, the values are flipped, which means we are actually performing a pure boolean conversion:
let myString1 = !!'take a look at';
let myString2 = !!'';
The double NOT (!!
) operator is kind of concise however does the identical factor because the Boolean
wrapper. Nevertheless, it’s kind of more durable to learn if you happen to’re not acquainted with the logical NOT (!
) operator.
We additionally must be cautious when utilizing this technique as an empty string with at the least one clean character will nonetheless return true
and once we attempt to convert a string of "false"
to a boolean worth of false
, it will nonetheless not work (simply as with Boolean
object).
Conclusion
On this article, we have taken a take a look at 4 methods to transform a string right into a boolean in JavaScript. The only means to take action is to make use of the strict equality operator to check our string worth to the "true"
– if the string is (strictly) equal to "true"
, the output will likely be boolean true
. Alternatively, you should use the ternary operator alongside the unfastened equality operator to realize the identical. Additionally, common expression matching is a stable method.
The final two strategies, Boolean
object and double NOT operator, have an easier syntax, however their downside is the way in which they deal with the false
worth – string “false” will return the boolean worth true
, which makes them relevant solely to a small subset of conversion circumstances.