Borislav Hadzhiev
Sun Feb 20 2022·3 min read
Photo by Toa Heftiba
To convert a string to a boolean in TypeScript, 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.
const str1 = 'true'; // 👇️ const bool1: boolean const bool1 = str1 === 'true'; console.log(bool1); // 👉️ true // ✅ If the string might be Uppercase or Title Case const str2 = 'False'; // 👇️ const bool2: boolean const bool2 = str2.toLowerCase() === 'true'; console.log(bool2); // 👉️ 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.
"true"
, the operator returns the boolean true
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.
If you need to convert a string to a boolean and consider only empty string
values to be false
and all other string values to be true
, use the Boolean
object.
const bool3 = Boolean('hello'); // 👉️ true const bool4 = Boolean(''); // 👉️ false const bool5 = Boolean('false'); // 👉️ true const bool6 = Boolean('true'); // 👉️ true const bool7 = Boolean(' '); // 👉️ true
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
.
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 (and TypeScript). 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
.
"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.
const bool8 = !!'hello'; // 👉️ true const bool9 = !!''; // 👉️ false const bool10 = !!'false'; // 👉️ true const bool11 = !!'true'; // 👉️ true const bool12 = !!' '; // 👉️ true
The double NOT (!!) operator converts empty strings to false
, all other
strings get converted to the boolean value true
.
Using the double NOT (!!) operator is the same as 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(!'hello'); // 👉️ 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
!
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.