Convert a string to a boolean in JavaScript
This post will discuss how to convert a string to a boolean in JavaScript.
1. Using Identity Operator (===)
The identity operator (===) compares two values and returns true if they are equal in both type and value, and false otherwise. To convert a string to a boolean using the identity operator, we need to compare the string with the value "true" or "false". This will return true if the string is exactly equal to "true" or "false", and false otherwise. Note that this comparison will be case-sensitive by default. To make it case-insensitive, we can convert the string to lowercase or uppercase first, using the String.prototype.toLowerCase() or String.prototype.toUpperCase() methods. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
const s = 'True'; var b; if (s.toLowerCase() === 'true') { b = true; } if (s.toLowerCase() === 'false') { b = false; } console.log(b); // true |
The above solution uses the strict equality operator (===), which performs a strict comparison with no implicit type conversion. Don’t use the equality operator (==), which converts the operands if they are not of the same type. The solution only parses strings with the value true or false. To extend the solution to parse other strings like yes, no, 1, 0, etc., we can do like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
function stringToBoolean(str) { if (typeof str !== 'string') { return undefined; } switch (str.toLowerCase()) { case "true": case "yes": case "1": return true; case "false": case "no": case "0": return false; default: return undefined; } } // Define a string that represents a boolean value let strings = ["true", "false", "yes", "no", "1", "0", "", 1, 0, null, undefined]; for (let s of strings) { console.log(s, stringToBoolean(s)); } |
Output:
true true
false false
yes true
no false
1 true
0 false
undefined
1 undefined
0 undefined
null undefined
undefined undefined
2. Using Regular Expressions (RegEx)
Another way to convert a string to a boolean in JavaScript is to use regular expressions (RegEx). To convert a string to a boolean using RegEx, we need to follow these steps:
- Create a RegEx pattern that matches the string values that we want to convert to true or false. We can use various characters and modifiers to create complex and precise patterns. For example, we can use the | character to create an OR condition, the
^and$characters to mark the beginning and end of the string, and the i modifier to make the pattern case-insensitive. - Use the
test()method, which returns true if the string matches the pattern, and false otherwise. Alternatively, we can use thematch()method, which returns an array of matches if the string matches the pattern, and null otherwise. - Use the result of the
test()ormatch()method as the boolean value.
Here is an example of how to implement these steps in JavaScript using a RegEx pattern that matches “true”, “yes”, or “1” as true values, and remaining values as false:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Define a string that represents a boolean value let strings = ["true", "false", "yes", "no", "1", "0", "", 1, 0, null, undefined]; // Create a RegEx pattern that matches true or false values let pattern = /^(true|yes|1)$/i; for (let s of strings) { // Use the `test()` method to check if the string matches the pattern let b = pattern.test(s); // Log the type and value of b console.log(s, b); } |
Output:
true true
false false
yes true
no false
1 true
0 false
false
1 true
0 false
null false
undefined false
That’s all about converting a string to a Boolean in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)