Converting String to Boolean in JavaScript
In this section, we will see converting string to boolean in Javascript. There are different methods in Javascript to do so. Let’s explore them one by one.
Using the Identity Operator
The identity operator is denoted by (===). The identity operator is also known as a strict equality operator. It returns true if both values are compared and are of the same type and have the same value. It sees if the value on the left-hand side is equal to the value on the right-hand side and returns true if it is and false if it is not. We can also use it to change a string to a boolean.
Essentially, we will compare our string to the " true " string. As a result, the output we get will be a boolean true only if our string is "true". Any other string will result in the function returning a false boolean value.
let sampleString1 = "true";
let booleanOutput1 = (sampleString1 === "true"); // This returns true
let sampleString2 = "Test";
let booleanOutput2 = (sampleString2 === "true"); // This returns false

You can also try this code with Online Javascript Compiler
Run Code
Using the Regular Expressions (RegEX)
Regular expressions (RegEx) are basically patterns used to match and test string character combinations.
For this blog, we'll utilize the most basic version of regular expressions in JavaScript - we'll write a simple regex that matches "true" and compare it to our text using the test() method.
This is case-sensitive, as it will return false if there is any tiny case inconsistency. To correct this, add /i to the end of the regular expression to ensure a case-insensitive match:
let stringValue = "true";
let boolValue = (/true/).test(stringValue); //This returns true
let stringValue = "True";
let boolValue = (/true/).test(stringValue); // This returns false
let stringValue = "True";
let boolValue = (/true/i).test(stringValue); // This returns true

You can also try this code with Online Javascript Compiler
Run Code
Using the Boolean Wrapper Class
For storing boolean values, JavaScript includes a built-in Boolean object. It's an object wrapper for boolean values, wrapping around other objects and transforming them into legal boolean values. This is accomplished by testing an object's true-false value. In general, empty items are assessed as false and non-empty objects as true.
Using the Boolean wrapper, any string that isn't an empty string will evaluate as true.
let String1 = Boolean('true'); // This returns true
let String2 = Boolean(''); // This returns false
let String3 = Boolean('false'); // This returns true
let String4 = Boolean('True'); // This returns true

You can also try this code with Online Javascript Compiler
Run Code
There are two key concerns at stake here:
-
The first is that this function will return true for an empty string containing at least one blank character (space, tab, etc.), which is why we must be cautious when using it.
- Second, transforming "false" to a boolean value of false fails because every non-empty string converts to true.
Using Double Not
-
The double NOT operator (!!) is equivalent to employing the logical NOT operator (!) twice, inverting the result of the single NOT operator.
-
When we use the double NOT operator, the values are flipped, resulting in a pure boolean conversion.
-
The double NOT (!!) operator is shorter but functions the same as the Boolean wrapper. It is, however, more difficult to read if you are unfamiliar with the logical NOT (!) operator.
-
We must also be cautious while using this approach because an empty string with at least one blank character will still return true, and attempting to convert a string of "false" to a boolean value of false will likewise fail (just as Boolean Object).
let myString1 = !'hello’; // This returns false
let myString2 = !''; // This returns true
let myString1 = !!'hello'; // This returns true
let myString2 = !!''; // This returns false

You can also try this code with Online Javascript Compiler
Run Code
Space and Time Complexity
The time and space complexity both are O(1) and O(1) respectively in all the ways mentioned to convert string to boolean.
Check out this problem - Longest Common Prefix
Must Read: Java System Out Println
Frequently Asked Questions
Can you convert a string to a boolean?
Yes, we can convert a string to a boolean in java by using methods such as Boolean.parseBoolean() or Boolean.valueOf().
How to pass string to boolean?
We can pass a string to boolean by using Boolean.parseBoolean() or Boolean.valueOf() method in java. This method accepts a string argument and returns a boolean value. The string is converted to ‘true’ or ‘false’.
How to convert string false to boolean?
We can use parseBoolean() method or compare the string to the lowercase string “false” using the equals() method to assign boolean values to convert string “false” to boolean value of “false”. For storing boolean values, JavaScript includes a built-in Boolean object. It's an object wrapper for boolean values, wrapping around other objects and transforming them into legal boolean values.
What is the string format for a boolean?
The format specifers %b or %B are used for formatting boolean expressions with String#printf(). These format specifers format the boolean value as a string. The resulting string will be “true” for a `true` value and “false” for a `false` value. `%b` format secifier gives result in lowercase string while `%B` gives the values in uppercase letters.
Conclusion
In this article, we have discussed the conversion of string to boolean in detail. We started with a basic introduction to strings and boolean values. Then we discussed the conversion in two programming languages, Java and JavaScript. In Java, we discussed two methods: Boolean.parseBoolean() and Boolean.valueOf(). In JavaScript, we discussed four ways (Using Identity operator, RegEx, boolean wrapper class, and double not) to change string to boolean.
To explore more interesting coding-related articles refer to the following links:
Recommended problems -
Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.
Keep learning, and Keep Growing!
Happy Learning!