Strings in JavaScript represent a sequence of multiple characters. A boolean is a data type that can have two possible values: true or false. In this article, we will learn about some easy methods used to convert string to boolean in JavaScript. Let us get started!
How to Convert String to Boolean in JavaScript?
This type of conversion is useful when dealing with conditional statements, comparisons, or logical operations that require boolean values. Sometimes, we get data from some user input or API, but in string format, that needs to be converted to boolean to do the accurately.
Let’s now look into various methods to convert a string to a boolean in JavaScript:
1) Using the Comparison Operator
One of the simplest methods to convert a string to a boolean value in JavaScript is by using the comparison (==) operator. As the name suggests, the comparison operator compares two operands and returns true if they are equal. We create a string containing the boolean value we want and then compare it to the given string.
Let us see an example of how to do it:
//declaring the string let str1="true"; let str2="false" //converting string to boolean let boolValue1=(str1=='true'); let boolValue2=(str2=='true'); //printing the bool values and checking the type console.log("The value of boolValue1 is "+boolValue1) console.log("The type of boolValue1 is " + typeof(boolValue1)); console.log("The value of boolValue2 is "+boolValue2) console.log("The type of boolValue2 is " + typeof(boolValue2));
Output:
The value of boolValue1 is true
The type of boolValue1 is boolean
The value of boolValue2 is false
The type of boolValue2 is boolean
In this example, we have two strings str1 and str2 having values “true” and “false” respectively. To convert the strings to boolean values, we use the comparison operator. Thus, we have obtained the desired output.
2) Employing the Boolean() Function
The Boolean() method is a built-in function we can use to convert a string to a boolean value. The Boolean() function converts the string and returns a bool value regarding its truthiness.
Here is an example:
//declaring the string let str1="true"; //converting string to boolean using the Boolean() function let boolValue1=(Boolean(str1)); //printing the type of boolValue1 console.log("The type of boolValue1 is " + typeof(boolValue1));
Output:
The type of boolValue1 is boolean
In this example, we convert a string to a boolean value using the Boolean() function. The string str1 having the value “true” is passed as an argument to the Boolean() function. The function returns the corresponding boolean value. Thus, we have obtained the desired result.
However, using Boolean() can make your code more explicit and readable when you specifically want to ensure a boolean result.
3) Using Regular Expressions
A regular expression is a pattern or a string enclosed between the forward slashes (/) representing rules or conditions for matching the strings. They are primarily used for matching strings to the pattern or regular expression specified. We can use regular expressions, to convert a string to a boolean by checking if it matches a specific pattern.
Let us take an example:
//declaring the string let str1="true"; //declaring the regular expression let regex=/^true$/; //converting string to boolean using the regular expression and test method let boolValue1 = regex.test(str1); //printing the type of boolValue1 console.log("The value of boolValue1 is " + boolValue1);
Output:
The value of boolValue1 is true
In this example, we use a regular expression /^true$/ which helps in matching the string “true”. The test() method helps in checking if the given string matches the pattern. It returns true if it matches the patterns and returns false otherwise.
Regex is also used for email validation in JavaScript.
4) Utilizing the Double NOT (!!) Operator
Using the double NOT operator is another technique to convert a string to a boolean. We simply apply the (!!) operator to a string to convert it to a boolean. See the example below:
//declaring the string let str1="true"; //converting string to boolean using the !! operator let boolValue1 = !!str1; //printing the type of boolValue1 console.log("The value of boolValue1 is " + boolValue1+" and its type is "+typeof(boolValue1));
Output:
The value of boolValue1 is true and its type is boolean
In this example, we have simply applied the Double NOT operator (!!) to the string which easily converts it into a boolean value. Thus, we have obtained the desired result.
5) Using JSON.parse()
The JSON.parse() method is a common method used to parse the JSON strings into their corresponding JavaScript objects. Hence, we can also use it to convert the string representation of a boolean to its boolean value. Here is the code for it:
//declaring the string let str1="true"; //converting string to boolean using the JSON.parse() method let boolValue1 = JSON.parse(str1); //printing the type of boolValue1 console.log("The value of boolValue1 is " + boolValue1+" and its type is "+typeof(boolValue1));
Output:
The value of boolValue1 is true and its type is boolean
In this example, we use the JSON.parse() method which takes str1 having value “true”, as an argument and returns the corresponding boolean value which is assigned to the variable boolValue1. We check the type and value of the boolValue1 variable. We have obtained the desired output.
Learn about some other methods to convert a JavaScript string to an object.
Conclusion
In this article, we have discussed various methods to convert a string to a boolean value in JavaScript. We can use the Comparison Operator, the Boolean function, the Regular Expressions, the Double Not Operator, or the JSON.parse() method to convert the string to boolean.