Convert a String to a Boolean in JavaScript


Borislav Hadzhiev

Last updated: Nov 30, 2021


Photo from Unsplash

Convert a String to a Boolean in JavaScript #

To convert a string to a boolean, use the strict equality operator to compare the string to the string "true", e.g. const bool = str === 'true'. If the condition is met, the strict equality operator will return the boolean value true, otherwise false is returned.

// ✅ If string is equal to "true" or "false" const str1 = 'true'; const bool1 = str1 === 'true'; // 👉️ true // ✅ If string might be Uppercase or Title Case const str2 = 'False'; const bool2 = str2.toLowerCase() === 'true'; // 👉️ false

The strict equality (===) operator checks if the values to the left and right hand side are equal and returns true if they are, and false otherwise.

If the string is equal to the string "true", the operator returns the booleantrue and assigns it to the variable, in all other cases, the value of the variable is false.

To convert any other string to a boolean, use the Boolean object.

Convert a String to a Boolean using Boolean #

To convert a string to a boolean, pass the string as a parameter to the Boolean() object, e.g. const bool = Boolean(str). The Boolean object converts the passed in string to a boolean. Empty strings get converted to false, in all other cases the result is true.

const bool3 = Boolean('test'); // 👉️ true const bool4 = Boolean(''); // 👉️ false const bool5 = Boolean('false'); // 👉️ true const bool6 = Boolean('true'); // 👉️ true const bool7 = Boolean(' '); // 👉️ true

We used the Boolean object to convert a string to a boolean.

Notice that the only scenario where the conversion returns false is if we pass an empty string to the Boolean object.

All other strings convert to true.

This is because the Boolean objects converts truthy values to true and falsy values to false.

All values that are not falsy are truthy in JavaScript. The falsy values are: false, null, undefined, 0, "" (empty string), NaN (not a number).

Notice that the empty string is a falsy value, this is why it gets converted to false.

If the string contains at least 1 character, be it an empty space, it's truthy and gets converted to true.

This approach would not work when trying to convert a string of "false" to a boolean value of false because any non-empty string converts to the boolean value true.

A more concise approach, which achieves the same result is to use the double NOT (!!) operator.

Convert a String to a Boolean using double NOT (!!) #

To convert a string to a boolean, use the double NOT (!!) operator, e.g. const bool = !!str. The double NOT (!!) operator converts empty strings to false, all other strings get converted to the boolean value true.

const bool8 = !!'test'; // 👉️ true const bool9 = !!''; // 👉️ false const bool10 = !!'false'; // 👉️ true const bool11 = !!'true'; // 👉️ true const bool12 = !!' '; // 👉️ true

The double NOT (!!) operator is basically using the logical NOT (!) operator twice.

The logical NOT (!) operator converts a value to a boolean and inverts the result. Here are some examples.

console.log(!'test'); // 👉️ false console.log(!''); // 👉️ true

The empty string is a falsy value, but when converted to boolean and flipped, we get true back.

To perform a pure boolean conversion, we have to flip the value again by using a second logical NOT (!) operator.

console.log(!!'test'); // 👉️ true console.log(!!''); // 👉️ false
The first ! converts the string to a boolean and flips the result. The second ! flips the boolean value, so we get a pure boolean conversion.

The double NOT (!!) operator is quite concise and does the same thing the Boolean object does, however it's a bit harder to read if you're not familiar with the logical NOT (!) operator.

Both ways of converting a string to a boolean are very common and often used throughout the same codebase.

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.