How to Convert TypeScript String to Boolean [6 Ways]

Here are the five ways to convert a string to a boolean in TypeScript:

  1. Using ternary operator
  2. Using a boolean map
  3. Using JSON.parse() function
  4. Using direct comparison with Strict Equality
  5. Using the boolean object
  6. Using Double Negation

Method 1: Using the ternary operator

You can use the “ternary operator” to check if the string value is equivalent to a truthy string (e.g., “true”) and return the corresponding boolean value.

Example

let strValue: string = 'true';
const strToBool = (s: string): boolean => s.toLowerCase() === 'true' ? true : false;
const boolValue = strToBool(strValue)

console.log(strValue)
console.log(typeof strValue)

console.log(boolValue)
console.log(typeof boolValue)

Output

Using the ternary operator

Method 2: Using a boolean map

You can create a mapping of string values to their boolean counterparts and then fetch the boolean value based on the input string.

Example

let strValue: string = 'true';

const strToBoolMap: { [key: string]: boolean } = {
  'true': true,
  'false': false
};

const strToBool = (s: string): boolean => strToBoolMap[s.toLowerCase()] || false;
const boolValue = strToBool(strValue)

console.log(strValue)
console.log(typeof strValue)

console.log(boolValue)
console.log(typeof boolValue)

Output

Using a boolean map

Method 3: Using JSON.parse() function

The JSON.parse() method is “used to convert certain string values to their respective boolean values.” This works best for “true” or “false” strings.

Example

let strValue: string = 'true';

const strToBool = (s: string): boolean => {
  try {
    return JSON.parse(s.toLowerCase());
  } catch (error) {
    return false;
  }
};

const boolValue = strToBool(strValue)

console.log(strValue)
console.log(typeof strValue)

console.log(boolValue)
console.log(typeof boolValue)

Output

Using JSON.parse() function

Method 4: Using direct comparison with Strict Equality

When you use strict equality to compare a string value, you check if the string is precisely equal to a specific value. In the case of boolean conversion, you’d typically check if the string is “true” or “false”.

Example

let strValue: string = 'true';

const strToBool = (s: string): boolean => s.toLowerCase() === 'true';

const boolValue = strToBool(strValue)

console.log(strValue)
console.log(typeof strValue)

console.log(boolValue)
console.log(typeof boolValue)

Output

Using direct comparison with Strict Equality

Method 5: Using the Boolean object

Using the Boolean object to convert a string to a boolean in TypeScript (or JavaScript) can be misleading if not used carefully. By default, the Boolean object will convert any non-empty string to true and an empty string to false.

Example

let strValue: string = 'true';

const strToBool = (s: string): boolean => Boolean(s);

const boolValue = strToBool(strValue)

console.log(strValue)
console.log(typeof strValue)

console.log(boolValue)
console.log(typeof boolValue)

Output

Using the Boolean object

Method 6: Using double negation

Double negation (!!) is a common JavaScript idiom to convert a value to its boolean representation. In TypeScript, which is a superset of JavaScript, this works similarly.

Example

let strValue: string = 'true';

const strToBool = (s: string): boolean => !!s;

const boolValue = strToBool(strValue)

console.log(strValue)
console.log(typeof strValue)

console.log(boolValue)
console.log(typeof boolValue)

Output

Using double negation

The behavior is similar to the direct use of the Boolean object. Any non-empty string is truthy in JavaScript, so it will be converted to true. An empty string is false, so it will be converted to false.

That’s it!

Related posts

TypeScript String to Boolean

TypeScript String to Number

TypeScript String to Enum

TypeScript Enum to String

Leave a Comment