Last updated: Feb 27, 2024
Reading timeยท5 min
To convert a string to a boolean, use the strict equality operator to compare
the string to the string "true"
.
If the condition is met, the strict equality operator will return the boolean
value true
, otherwise false
is returned.
// โ if the string is equal to "true" or "false" const str1 = 'true'; const bool1 = str1 === 'true'; // ๐๏ธ true // โ if the string might be Uppercase or Title Case const str2 = 'False'; const bool2 = str2.toLowerCase() === 'true'; // ๐๏ธ false
If you need to convert a boolean to a string, click on the following subheading:
The
strict equality (===)
operator checks if the values on the left and right-hand sides are equal and
returns true
if they are, and false
otherwise.
If the string is equal to the string "true"
, the operator returns the boolean
true
and assigns it to the variable.
In all other cases, the value of the variable is false
.
If you have to do this often, define a reusable function.
function strToBool(str: string) { return str.toLowerCase() === 'true'; } console.log(strToBool('true')); // ๐๏ธ true console.log(strToBool('True')); // ๐๏ธ true console.log(strToBool('false')); // ๐๏ธ false console.log(strToBool('abc')); // ๐๏ธ false
The function takes a string as a parameter and converts it to its boolean equivalent.
To convert any other string to a boolean, use the Boolean
object.
You can also use the RegExp.test()
method to convert a string to a boolean.
function strToBool(str: string) { return /true/.test(str); } console.log(strToBool('true')); // ๐๏ธ true console.log(strToBool('True')); // ๐๏ธ false console.log(strToBool('false')); // ๐๏ธ false console.log(strToBool('abc')); // ๐๏ธ false console.log(strToBool('is true')); // ๐๏ธ true
The
RegExp.test
method returns true
if the regular expression is matched in the string and
false
otherwise.
We called the test()
method on a regular expression.
The forward slashes / /
mark the beginning and end of the regular expression.
If the string "true"
is contained in the string, we get a match.
If you only want to return true
if the entire string is equal to "true"
,
update the regex.
function strToBool(str: string) { return /^true$/.test(str); } console.log(strToBool('true')); // ๐๏ธ true console.log(strToBool('True')); // ๐๏ธ false console.log(strToBool('false')); // ๐๏ธ false console.log(strToBool('abc')); // ๐๏ธ false console.log(strToBool('is true')); // ๐๏ธ false
The caret ^
symbol matches the beginning of the input and the dollar sign $
matches the end of the input.
If you want to make the regular expression case-insensitive, use the i
flag.
function strToBool(str: string) { return /^true$/i.test(str); } console.log(strToBool('true')); // ๐๏ธ true console.log(strToBool('True')); // ๐๏ธ true console.log(strToBool('false')); // ๐๏ธ false console.log(strToBool('abc')); // ๐๏ธ false console.log(strToBool('is true')); // ๐๏ธ false
The i
flag allows us to perform a case-insensitive search in the string.
To convert a string to a boolean, pass the string as a parameter to the
Boolean()
constructor.
Empty strings get converted to false
, in all other cases the result is true
.
const bool1 = Boolean('test'); // ๐๏ธ true const bool2 = Boolean(''); // ๐๏ธ false const bool3 = Boolean('false'); // ๐๏ธ true const bool4 = Boolean('true'); // ๐๏ธ true const bool5 = Boolean(' '); // ๐๏ธ true
We used the Boolean constructor to convert a string to a boolean.
false
is if we pass an empty string to the Boolean
constructor.All other strings convert to true
.
This is because the Boolean
constructor converts truthy values to true
and
falsy values to false
.
The falsy values are: false
, null
, undefined
, 0
, ""
(empty string),
NaN
(not a number).
All other values are truthy.
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
.
console.log(Boolean(' ')); // ๐๏ธ true
Therefore, this approach wouldn't work when trying to convert a string of
"false"
to a boolean value of false
because all non-empty strings convert to
the boolean value true
.
A more concise approach, which achieves the same result is to use the double NOT (!!) operator.
The double NOT (!!) operator converts empty strings to false
and all other
strings to the boolean value true
.
const bool1 = !!'test'; // ๐๏ธ true const bool2 = !!''; // ๐๏ธ false const bool3 = !!'false'; // ๐๏ธ true const bool4 = !!'true'; // ๐๏ธ true const bool5 = !!' '; // ๐๏ธ true
The double NOT (!!) operator is the same as using the logical NOT (!) operator twice.
The logical NOT (!) operator:
Here are some examples.
console.log(!'test'); // ๐๏ธ false console.log(!''); // ๐๏ธ true
The empty string is a falsy value, but when converted to a boolean and flipped,
we get a true
value 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 as the
Boolean()
constructor.
However, it's a bit harder to read if you aren't 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.
Use the String()
constructor to convert a boolean to a string in TypeScript.
The String()
constructor converts the passed-in value to a string
and
returns the result.
// ๐๏ธ const bool: true const bool = true; // ๐๏ธ const str: string const str = String(bool); console.log(str); // ๐๏ธ "true" console.log(typeof str); // ๐๏ธ "string"
The String() constructor takes a value as a parameter and converts it to a string.
console.log(String(true)); // ๐๏ธ "true" console.log(String(false)); // ๐๏ธ "false"
Alternatively, you can use the toString method to convert a boolean to a string.
const bool = true; console.log(bool.toString()); // ๐๏ธ "true" console.log(typeof bool.toString()); // ๐๏ธ "string"
The Boolean.toString()
method returns the string representation of the boolean
value.
Another common way to convert a boolean to a string is to interpolate the boolean in a string using a template literal.
const bool = true; console.log(`${bool}`); // ๐๏ธ "true" console.log(typeof `${bool}`); // ๐๏ธ "string"
Template literals are delimited with backticks and allow us to embed variables
and expressions using the dollar sign and curly braces ${expression}
syntax.
The dollar sign and curly braces syntax allows us to use placeholders that get evaluated.
const num = 50; const result = `${num + 50} percent`; console.log(result); // ๐๏ธ 100 percent
Another common way to convert a boolean value to a string is to use the ternary operator.
const bool = true; const str2 = bool ? 'true' : 'false'; console.log(str2); // ๐๏ธ "true" console.log(typeof str2); // ๐๏ธ "string"
The ternary operator is very similar to an if/else
statement.
If the boolean value is true
, then the string true
is returned, otherwise,
the string false
is returned.
This is convenient because there are only 2
possible boolean values, so a
shorthand if/else
statement gets the job done.