Convert a String to a Boolean in JavaScript

avatar

Borislav Hadzhiev

Tue Nov 30 20213 min read

banner

Photo by Erik Mclean

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.

index.js
// โœ… 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.

index.js
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 Boolean #

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.

index.js
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.

index.js
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.

index.js
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 code base.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee