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.

let isActiveStr: string = 'false';
let isActive: boolean = isActiveStr == 'true' ? true : false;

let statusStr: string = 'true';
let status: boolean = statusStr == 'true' ? true : false;

when we run the above code, we get converted value in console check below output.


TypeScript, convert string to boolean example
TypeScript, convert string to boolean example

All the best 👍.