Awena
Awena

Reputation: 1022

javascript numeric variable display 0 instead of null

How do you make a numeric variable in javascript return 0 instead of null ?

I have a statistics table for users queries.. When a search criteria is 0 it is returned as null in the JSON object and displayed null in my table.. I was wondering if there's any workaround

e.g SELECT SUM( CASE WHENtable.color= 'red' THEN 1 ELSE 0 END ) AS 'Red', SUM( CASE WHENTable.color= 'Blue' THEN 1 ELSE 0 END ) AS 'Blue', ect....

var RedShirts = Stats.Red ;
$("#Red").text(RedShirts);

It bothers me when it displays null instead of 0.. Thanks

Upvotes: 0

Views: 122

Answers (3)

Namila
Namila

Reputation: 754

try

if(RedShirts){
      $("#Red").text(RedShirts);
}

else{
 $("#Red").text("0");}

Upvotes: 0

Derek 朕會功夫
Derek 朕會功夫

Reputation: 94319

Try

$("#Red").text(+RedShirts);

Upvotes: 1

hsz
hsz

Reputation: 152206

Try with:

$("#Red").text(RedShirts || 0);

Upvotes: 1

Related Questions