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';
console.log(isActiveStr);
let isActive: boolean = isActiveStr == 'true' ? true : false;
console.log(isActive);
let statusStr: string = 'true';
console.log(statusStr);
let status: boolean = statusStr == 'true' ? true : false;
console.log(status);
when we run the above code, we get converted value in console check below output.
Output
All the best 👍.