Pornima Patil
Pornima Patil

Reputation: 207

How to convert boolean true or false into 1 Or 0 in javascript

function getReadyStateHandler(xmlHttpRequest) {
  return function() {
    if (xmlHttpRequest.readyState == 4) {
      if (xmlHttpRequest.status == 200) {
        var jsonString = xmlHttpRequest.responseText;
        var jsonObject = (new Function("return " + jsonString))();
        var exportVal = jsonObject.exportValue;
      }
    }
  }
}

This is javascript code.it returns true and false value but i want to print 1 and 0.How to solve this problem,please help me.

function getReadyStateHandler(xmlHttpRequest) {
    return function() {
        if (xmlHttpRequest.readyState == 4) {
            if (xmlHttpRequest.status == 200) {
                var jsonString = xmlHttpRequest.responseText;
                var jsonObject = (new Function("return " + jsonString))();

                var accountid = jsonObject.accountid;

                var exportVal = +jsonObject.exportValue;

                if (accountid.length > 0) {

                    dispTable(accountid, exportVal);

                } 


        }
    };
}

function dispTable(accountid,exportVal) {

    $("#tbl tbody tr").eq(0).remove(); 

    for (var i = 0; i < accountid.length; i++) {
        var temp = parseInt(i) + 1;
        $('#tbl > tbody:last')
                .append(
                        '<tr>' + "<td class='numeric'>"+ temp+ "</td>"
                                + "<td class='numeric'>"+ accountid[i]+ "</td>"
                                + "<td class='numeric'>"+ exportVal[i]+ "</td>"
                                + "<td  class='numeric'>"


    }


}

This is my new code.in which i have displayed the table in which value is displayed as true and false.

Upvotes: 2

Views: 733

Answers (3)

Sankar
Sankar

Reputation: 7107

Simply, use Number Function

var s = false;
alert(Number(s));

For your requirement,

 var exportVal = Number(jsonObject.exportValue);

Upvotes: 3

Rahul Tripathi
Rahul Tripathi

Reputation: 172408

You can try to use the ternary operator like:

var yourreturnValue = result ? 1 : 0;

Upvotes: 0

antyrat
antyrat

Reputation: 27765

You can use + sign:

var myVar = true;
myVar = +myVar; // 1

myVar = false;
myVar = +myVar; // 0

So if your jsonObject.exportValue is boolean you can just convert it by:

var exportVal = +jsonObject.exportValue;

Upvotes: 5

Related Questions