If you intend to cast the string “true” to Boolean true and the string “false” to Boolean false, then your best bet is to use the strict equality operator.

const trueString = 'true';
const trueBoolean = trueString === 'true'; // true

const falseString = 'false';
const falseBoolean = falseString === 'true'; // false

Please note that the check above is case-sensitive. If you are dealing with user input or you are uncertain about the contents of the string, you might need to ignore the case. To do this, you can always use the toLowerCase() method as follows:

const capitalTrueString = 'True';
capitalTrueString === 'true'; // false, ouch!

capitalTrueString.toLowerCase() === 'true'; // true

In the example above, I used the toLowerCase() method to transform the string “True” to lowercase first so that when I compare it the string “true”, it returns Boolean true.

If you look closely at the code above, you will notice that it returns Boolean true only for a “true” string, but it returns Boolean false for any other string, not necessarily the string “false”. If you use an empty string “”, a string missing a letter “fals”, or even “Hello World”, it will return a Boolean false for all of these.

Normally, this is intended behavior. In most cases, you would only want to make sure that a string is “true”. If it’s anything other than this, you can consider it to be false.
However, if you need to be extra accurate about it, you can use a function like this:

function stringToBool(string) {
	const lowerCaseString = string.toLowerCase();

	if (lowerCaseString === 'true')
		return true;
	else if (lowerCaseString === 'false')
		return false;
		
	return null;
}

This function works as follows:

  1. When given a string “true”, “True”, “tRuE”, or any other variation, it returns Boolean true.
  2. When given a string “false” or any variation of it, it returns Boolean false.
  3. When given anything else, it returns null.

Truthy and Falsy Strings

If your intention is not to parse the strings “true” and “false” to their matching Boolean values, but instead, get the Boolean equivalent of a random string, then you have to know about truthy and falsy strings first.

For the purposes of this post, the only falsy string that exists is the empty string "" or ''. Any other string is considered a truthy string.
If you are interested, I explained truthy and falsy values in more detail in my JavaScript double exclamation mark guide.

To check for truthy and falsy strings, you can use one of the following 3 methods:

Double Bang Operator (!!)

This operator, also known as the logical NOT operator applied twice, is the most concise and commonly used approach. It coerces the operand to a boolean value, effectively converting truthy values to true and falsy values to false.

const str1 = "hello";
const str2 = "";
const str3 = "0";

console.log(!!str1);  // true
console.log(!!str2);  // false
console.log(!!str3);  // false (falsy due to type coercion)

Boolean Constructor (Boolean(string))

The Boolean constructor explicitly creates a boolean object from the provided argument. While it offers type safety, it essentially performs the same type coercion as the double bang operator.

    const str1 = "hello";
    const str2 = "";
    const str3 = "0";
    
    console.log(Boolean(str1));  // true
    console.log(Boolean(str2));  // false
    console.log(Boolean(str3));  // false (falsy due to type coercion)

    Comparison Operators (== or !=)

    Comparing a string with true or false using these operators implicitly converts the string to a boolean value based on truthy/falsy rules. While functional, this approach can be less readable and might lead to unintended consequences if not used cautiously.

    const str1 = "hello";
    const str2 = "";
    const str3 = "0";
    
    console.log(str1 == true);  // true
    console.log(str2 == true);  // false
    console.log(str3 != true);  // true (falsy due to type coercion)

    Similar Posts