Hi Friends 👋,
Welcome to Infinitbility ❤️!
Today, we are going to see how we can convert string to boolean in typescript, here we are going to use the ternary operator to convert string to boolean.
To convert a string to a boolean, we have to check whether 'true'
is present in the string or not, and if present we have to assign a new true
value or false
.
In the following code, we are taken of both cases if the string true
or if string false
.
1let isActiveStr: string = 'false';2console.log(isActiveStr);3let isActive: boolean = isActiveStr == 'true' ? true : false;4console.log(isActive);56let statusStr: string = 'true';7console.log(statusStr);8let status: boolean = statusStr == 'true' ? true : false;9console.log(status);
when we run the above code, we get converted value in console check below output.
Output

All the best 👍.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.