Tuesday, August 7, 2018

How to Convert String to boolean or boolean to string in typescript /javascript with example

String and boolean conversion tutorial

During development, We have a use case where strings like 'true' and false are needs to convert to boolean. or  On Frontend we want to perform conditions - if else on converting boolean/string to display some results.

In typescript/javascript string have a string and boolean are objects. both serving for a different purpose and their values also different.

In this article, Publishing different ways to convert string to boolean or boolean in typescript.
We want string formatted value need to convert toa boolean value.

How to convert String to boolean in typescript

I am listing out different ways with examples

Using String equality example

suppose String object holds 'true' or 'false', We will use string equality check to convert this to boolean.
let inputTrueString = "true";
let inputFalseString = "true";
let inputNotBooleanString = "asdfasdfasd";
let truthValue= (inputTrueString === 'true'); 
let falseValue = (inputFalseString === 'false');
let notBooleanValue=(inputNotBooleanString === 'false');
console.log(truthValue)// outputs true
console.log(falseValue) // outputs false
console.log(notBooleanValue) // outputs false
The only disadvantage of this approach is it is returning false for 'false' and 'asdfasdfasd' string values. Here false means we can't identify with false is a boolean value or string value This will be helpful in conditional logic validations for checking the string to boolean conversion 

Using regular expression example

Using regex pattern and test method, we can convert to boolean test() method is used to test match a pattern in a string if matched returned true, else returns false
(/true/i).test('true') //outputs true
(/true/i).test('false') //outputs false
(/true/i).test('asdfasdfads') //outputs false

JSON Parse method example 

This is one more way of converting boolean string values to string format. parse method of JSON object which accepts string returns true if the value is 'true' returns false if the value is 'false' throws an exception if the value is string other than boolean values We have written function to return undefined for normal strings.
console.log(stringToBoolean('true')) // undefined
console.log(stringToBoolean('false')) // outpus undefined
console.log(stringToBoolean('asdfasdfasd')) //outputs undefined
function stringToBoolean(stringValue: string): boolean | undefined {
    try {
        return JSON.parse(stringValue);
    }
    catch (e) {
        return undefined;
    }
}
How to convert boolean to string in typescript/javascript? It is very easy to convert boolean to string. using toString() method example Any object in typescript/javascript has toString() method which extends from the object which returns the string version
let booleanTrueVariable: boolean = true;
let booleanFalseVariable: boolean = false;
let stringTrue: string = booleanTrueVariable.toString();
let stringFalse: string = booleanFalseVariable.toString();
console.log(stringTrue) // outputs true
console.log(typeof stringTrue) // outputs string
console.log(stringFalse) // outputs false
console.log(typeof stringFalse) // outputs string

Using ternary Operator example

This is a simple way to convert to a boolean string. The ternary operator takes three arguments first condition if true, returns the second argument, if false -return the third argument here is an example to do it

let booleanTrueVariable: boolean = true;
let booleanFalseVariable: boolean = false;
let stringValue = booleanTrueVariable? "true":"false";
let stringTrue: string =booleanTrueVariable? "true":"false";
let stringFalse: string = booleanFalseVariable? "true":"false";
console.log(stringTrue) // outputs true
console.log(typeof stringTrue) // outputs string
console.log(stringFalse) // outputs false
console.log(typeof stringFalse) // outputs string
That's it about converison of boolean and strings in javascript. Please like and comment faceook/twitter and subscribe for early access of this tutorials.


EmoticonEmoticon

Note: Only a member of this blog may post a comment.