We may have a boolean
value in string format (‘true’ or ‘false’) and we want its boolean
equivalent (such as true
or false
) ot parse this string to boolean form.
Practical application is when we have a check condition value which is stored as a string in database and based on this value we want to perform some operation on our html page (or jsp).
This article will explain 5 different ways to convert string to boolean in javascript with example code.
1. test() method
test()
method of javascript matches a regular expression against a string. Example,
let stringValue = "true"; let boolValue = (/true/i).test(stringValue) //returns true
In this case we check whether the string contains true or not. Now, if the string value is ‘true’, then boolValue will be true
, else a false
./i
at the end of regular expression is for case insensitive match.
2. Comparison operator
Comparison operator in javascript is a double equal to sign which compares the values of the operands on its right and left sides.
It returns true
if both the operands have the same value and false
otherwise.
let stringValue = "true"; let boolValue = (stringValue == "true"); //returns true
If we use ==
operator to check the string value against ‘true'(in string format).
If the string value is ‘true’, then the boolean value will also have a true
, else a false
.
Javascript also has another equality operator, ===
.
Do you know the difference between == and === ?
3. JSON.parse()
Here we use the parse()
method of JSON
object.
JSON
is a built-in javascript object and its parse()
method parses a string as JSON and converts it into an object corresponding to the given text.
So, if the string value is 'true'
, it is converted to a boolean
object with value as true
and false
otherwise.
let stringValue = "true"; let boolValue = JSON.parse(stringValue); //returns true
Tired of reading… Watch video!!!
4. Ternary operator
Here, we simply check the string for its equality with 'true'
and return a boolean true
if there is a match and false
otherwise using a ternary operator.
let stringValue = "true"; let boolValue = stringValue.toLowerCase() == 'true' ? true : false; //returns true
5. switch-case
A switch-case construct is used to cover all possible string value combinations which should lead to a true
boolean value.
These string values may be ‘true’, ‘1’, ‘on’, ‘yes’. It also covers boolean true
or numeric 1.
If the given string (‘true’) or any other values match, then the boolean true
is returned while boolean false
is returned in every other case.
let stringValue = "true"; let boolValue = getBoolean(stringValue); //returns true function getBoolean(value){ switch(value) { case true: case "true": case 1: case "1": case "on": case "yes": return true; default: return false; } }
6. Boolean Object
You can convert a string to boolean in javascript using Boolean
object as shown below
let b = Boolean("true"); // b will be true
But do not consider writing Boolean("false")
to convert it to false
.
Boolean wrapper returns following boolean values according to argument types as
Boolean(“true”) will be true.
Boolean(“false”) will be true.
Boolean(“”) will be false.
Boolean(” “) will be true.
Boolean(undefined) will be false.
Boolean(NaN) will be false
Boolean(null) will be false.
This article explained different methods to parse a string to a boolean in javascript.
Hope it was useful.