In simple terms, each data type stores a specific type of data and serves a special purpose. However, the user can also convert the data from one type to another as per requirements.
This post explains all the possible approaches to convert a string into a boolean in TypeScript. The guideline of this post is as follows:
- Using the “strict equality” Operator
- Using the Boolean Constructor
- Using the “!!(double exclamation mark)” Operator
- Using a Regular Expression
- Use the “parse()” Method of JSON
- Using a “switch” Statement
- Using a “ternary” Operator
Let’s start with the “strict equality” Operator
Method 1: Using the “strict equality(===)” Operator
The “strict equality(===)” operator checks whether the specified two operands are equal or unequal and returns the result as a boolean value i.e. true/false. In this example, the “strict equality” operator is applied for converting the specified string into a boolean.
Code
Copy the given lines of code into the file having the “.ts” extension:
const bool = str.toLowerCase() === 'true';
console.log(bool);
In this code:
- The “str” variable initializes a string quoted in single quotes.
- Next, the “bool” variable applies the “toLowerCase()” method to convert the initialized string into lowercase and then check whether it is equal to the given operand or not.
- After that, the “console.log()” method displays the “bool” variable value.
Output
Now, compile the “.ts” file and run the automatically generated “.js” file to see the output using the following commands:
node main.js //Run
The output shows that the specified string has been successfully converted to a boolean value i.e. “true”.
Method 2: Using the Boolean Constructor
The “Boolean()” constructor is also beneficial to convert a string into a boolean. It specifies the string as its argument and then retrieves a boolean value.
Code
console.log(value1);
const value2 = Boolean('false');
console.log(value2);
In the above code block:
- The “value1” variable uses the “Boolean()” constructor having the string as its argument.
- The “console.log()” method shows the “value1” variable result.
- The same procedure is followed for another string stored in the “value2” variable.
Output
Compile “.ts” and run the “.js” file:
node main.js //Run
The above output returns a “true” boolean value for both of the initialized strings.
Method 3: Using the “!!(double exclamation mark)” Operator
The “!!(double exclamation mark)” acts as a double not operator that converts the specified object into a boolean value and returns “true”. Here in this scenario, it is used for the conversion of a string into a boolean.
Code
const value = !!str;
console.log(value);
At this time the “!!” operator is associated with the “str” variable to convert its value i.e. string into boolean.
Output
Execute the compiler and run the “.js” file:
node main.js //Run
The outcome displays that the initialized string has been successfully converted into a boolean i.e. “true”.
Method 4: Using a Regular Expression
The “test()” method of the “Regular” interface allows the user to create a regular expression as per requirement. In this method, it is used to create a “/true/i” regex to convert a string into a boolean. In this regex, the “true” represents a pattern and the “i” specifies the case-insensitive flag.
Code
const value = (/true/i).test(str);
console.log(value);
Here, the created “(/true/i)” regular expression is used with the “test()” method that takes the “str” variable as its argument. As a result, it will convert the given string and retrieve a boolean value.
Output
The output shows a “true” boolean value as a converted string because the specified string matches with the regex pattern “true”.
Method 5: Use the “parse()” Method of JSON
The “parse()” method of the JSON interface helps to transform/parse a JSON string in TypeScript. This scenario uses the stated method to parse the specified string into a boolean.
Code
const value = JSON.parse(str);
console.log(value);
Now, the JSON “parse()” method takes the “str” variable as its argument to parse its string value into a boolean.
Output
The above output shows the converted string into a boolean i.e. “true”.
Method 6: Using a “switch” Statement
The “switch” statement is used to check different cases in TypeScript. Here it is utilized for converting the string into a boolean depending on the cases.
Code
let value: boolean;
switch(String) {
case 'true':
value = true;
break;
case 'false':
value = false;
break;
}
console.log(value);
The above code snippet specifies the “switch” statement which returns the “boolean” type value depending on the specified cases.
Output
The outcome displays the boolean value “false” according to the declared value of the string type.
Method 7: Using a “ternary” Operator
The “ternary” operator refers to a conditional operator that is the simplest/shortest way of specifying an “if-else” statement in TypeScript. In this case, it is used to convert a string into a boolean. Let’s see it practically.
Code
const bool = str === 'false' ? true : false;
console.log(bool);
Here, in the above lines of code:
- The “ternary” operator first specifies the condition followed by “?(question mark)”, and then the first and second expressions that are separated by a “:(colon)”.
- If the specified condition becomes “true” the first expression “true” will execute and if the condition becomes “false” then the second “false” expression will execute.
Output
The output returns “true” as a converted string because the specified condition became true.
Conclusion
To convert a “string” into a “boolean” in TypeScript use the “strict equality”, “!!(double exclamation mark)”, and “ternary” operators as well as the “Boolean” constructor. This task can also be performed with the help of the “Regular Expression”, JSON “parse()” method, and the “switch” statement. All of the discussed approaches are quite simple and easy to use. This post explained all possible methods practically to convert a string into a boolean in TypeScript.