How to convert string to Boolean in javascript?

13,138

Solution 1

You can use JSON.parse('true');

JSON.parse(isShow.toLowerCase());

Try the example below.

var result = ['True', 'False']

var isShow = result[Math.round(Math.random())];

console.log(JSON.parse(isShow.toLowerCase()));

Solution 2

If you know it's always going to return the string True and False, you could just use;

window.MyWindow.IsFull = '<%=this.IsFull%>' === "True";
Share:
13,138
gene
Author by

gene

Working with .NET for the past 5 years. Worked with VB and JAVA. After work releasing stress by working out. Love outdoor activities like fishing, camping and hiking

Updated on June 24, 2022

Comments

  • gene
    gene almost 2 years

    I have a public property returned from my code-behind class to aspx page:

    window.MyWIndow.IsFull = '<%=this.IsFull%>';
    

    In code-behind it is defined this way:

    public bool IsFull
    {
        get;
        set;
    }
    

    Now, when I use it in my javascript file, I have the following code:

    var isShow = myself.IsFull;
    

    This way isShow is either 'True' or 'False'

    I need to convert it on the page level, so isShow is boolean instead of string.

    So I can write the if else logic

    How can I do it?