Convert String to Boolean in JavaScript

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

There are times when we have a string that contains "true" or "false" and we need to convert these strings to boolean (or bool) in Javascript. To do so, we have nine different ways in which we can convert string to boolean in JavaScript.

Introduction

In programming, sometimes boolean values (true and false) are stored as strings ("true" and "false"). It usually happens when we take string input from users or when we read files. If we wish to perform boolean operations on these values, these strings need to be converted into boolean values. There are various in-built functions that help convert strings to boolean in Javascript. In this article, we will learn about these methods.

Convert String to Boolean in JavaScript

Following are the different ways in which we can convert strings to boolean values in JavaScript.

Note: In the following sections, a string value (containing true or false) is written within quotes, i.e. "true" or "false", while a boolean value is written without quotes, i.e. true or false.

1. Using test() Method

The test() method in JavaScript uses regular expressions to match the given string. This method returns true if a match is found and false otherwise. The test() method is a method of the RegExp object.

In order to convert a string to a boolean using the test() method, we simply need to match a regular expression object containing true with the given string. If the given string contains "true", the test() method will return true. In any other case, this method will return false.

For example:

Output:

In the above example, in line 5, we used RegExp() to convert the string "true" to a regular expression object. This object was required to call the test() method. Once the object was created, we simply called the test() method to check if the given strings str1 and str2 contained"true" or not. Because str1 contained "true" and str2 contained "false", the test() function returned boolean true and false, respectively.

2. Using Comparison Operator

The comparison operator (==) in JavaScript compares the operands present on its left and right sides. If the value of both operands is the same, the comparison operator returns true. Otherwise, it returns false.

To convert a string to a boolean with the help of the comparison operator, we need to create a string that contains "true". Then, we just need to compare this string with the given string. If the given string contains "true", we will get a boolean true value. In any other case, we will get false.

For example:

Output:

In the above example, we needed to convert the string stringToConvert to a boolean value. To do so, we defined another string called stringToCompare that stored "true". Then, we used the comparison operator to compare these two strings. In this case, because the two strings were different, the == operator returned false. This returned value was stored in the boolVal variable. Finally, we printed the returned value and the data type of this value.

3. Using JSON.parse()

The JSON.parse() method takes a string as input and returns an object that corresponds to the input string. So, if a string containing "false" or "true" is passed to this method, the JSON.parse() method will convert this string to a boolean object.

For example:

Output:

In this example, in line 6, we used JSON.parse() to convert the string str to a boolean. As str contained "false", JSON.parse() returned boolean false. Then, we printed the value returned by this method.

4. Using Identity Operator (===)

The identity operator (or the strictly equal operator) in JavaScript can also be used to convert a string to a boolean value. To convert a string to a boolean, we use this operator in the same way as we use the comparison operator. The identity operator returns true if the left-hand side and the right-hand side are equal, and false otherwise.

Note: Although there are some differences between the identity and the comparison operator, in the case of converting a string to a boolean, these two operators essentially behave in the same manner.

For example:

Output:

Here in this example, we wanted to convert the string stringToConvert to a boolean. In line 3, we defined a string stringToCompare that stored "true". Then, we used the identity operator to compare the two strings. Since both these strings had the same text, the identity operator returned true.

5. Using the Boolean Wrapper Class

The Boolean object is an object wrapper class in JavaScript. It wraps around objects to convert them into boolean values. It takes one object as its argument. If this object is empty, Boolean returns false. If this object is not empty, Boolean will return true, no matter what is present in that object.

Note: It is not recommended to use the Boolean wrapper class to convert strings into boolean values because, even if the string contains "false", using Boolean will return true because the string was not empty.

For example:

Output:

In this example, we used the Boolean wrapper class to convert strings to a boolean. As we can observe, when we passed an empty string to Boolean, the string was converted to false. However, when we passed non-empty strings "true" and "false" to Boolean, both these strings were converted into boolean true.

6. Using Ternary Operator

The ternary operator takes 3 operands - a conditional statement, an expression to execute if the conditional holds correct, and the expression to execute if the conditional is not correct. In order to convert a string to a boolean using this method, the conditional statement would compare the given string with "true". If the conditional is correct, the ternary operator would return true. Else, it will return false.

For example:

Output:

Here in this example, we wanted to convert the string str to bool. In line 6, we used the ternary operator with three parameters. The first parameter (i.e. the conditional statement) compared str with another string "true" using the == operator. The second parameter returned the boolean value true and the third parameter returned the boolean value false. In our case, since str contained "false", the ternary operator's conditional became incorrect. Hence, false was returned by the ternary operator. Finally, we printed the value returned.

7. Using Switch-case

The switch-case statement provides us with the benefit to convert different string values to boolean values. Using this, we can even convert strings that contain "True", "1", etc. to boolean true. Similarly, we can cover different scenarios for false as well.

For example:

Output:

Here in this example, we wanted to convert two strings str1 and str2 into boolean values. To do so, we defined a function scFunc() that contains a switch-case statement. In the switch-case statement, we defined three different cases - "true", "1", and "yes". If the given string had been any of these three values, the function scFunc() would have returned true. For any other string values, false would have been returned.

In our example, str1 had the value "0", while str2 had the value "yes". So, these strings were converted to false and true respectively.

8. Double NOT Operator (!!)

The double NOT (!!) operator works like the Boolean wrapper class. It converts every non-empty string to true and every empty string to false.

Note: It is not recommended to use the double NOT operator to convert strings into boolean values because, even if the string contains "false", using this operator will return true because the string was not empty.

For example:

Output:

From the above example, we can see that when we tried to convert an empty string ('') to a boolean, the !! operator converted that string to false. Also, when we tried to convert both "true" and "false" strings to boolean, in both cases, the !! operator converted these strings to true. This is why we should avoid using this operator while converting strings to boolean values. Use this JavaScript Formatter to format your code.

Conclusion

  • If a boolean value is stored in a string, JavaScript provides multiple methods through which we can convert that string into a boolean value.
  • The Boolean wrapper class and the double NOT operator are not recommended to convert strings to boolean values.
  • The switch-case statement allows us to convert different types of string values to boolean values.
  • The test() method uses regular expressions to match the text in a string. It returns true if a match is found and false otherwise.
  • The comparison and identity operators compare the operands present on their left-hand and right-hand sides. If both operands are the same, these operators return true.
  • The JSON.parse() method converts a string to its corresponding object type. So, if we pass a string containing boolean text, it will convert that string to a boolean value.