Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string to Boolean in javascript?

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?

like image 422
gene Avatar asked Feb 04 '26 00:02

gene


2 Answers

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()));
like image 89
Paul Fitzgerald Avatar answered Feb 05 '26 13:02

Paul Fitzgerald


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

window.MyWindow.IsFull = '<%=this.IsFull%>' === "True";
like image 32
Shadow Avatar answered Feb 05 '26 14:02

Shadow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!