How to check if variable is containg data or not in javascript

时间:2016-10-19 13:39:24

标签: javascript

I have 2 variable 'aassn' and 'addd' in xml format. my concern is if 'Idc' of 'aassn' is blank then dont need 'Idc' value in output and same if 'Idd' of 'addd' is blank then dont need in output. If, 'Idc' and 'Idd' contaning data then need both in output. where aassn=

<?xml version="1.0" encoding="UTF-8"?>
<info>
 <name>
  <Idc>81</Idc>
 </name>
</info>

and addd=

my code is

var ssn1 = aassn.match(/<Idc\/>/);
var acc1 = addd.match(/<Idd\/>/);
var tempaassn = String(aassn);
var tempacc = String(addd);
var star1 = "Hi,\n\nDetails";

if (typeof(ssn1) != 'undefined' && ssn1 != '' && String(ssn1) != '' && tempaassn.indexOf('</Idc>') > -1) 
star1+= "\n\nBelow first data:\n\n" +  aassn.split("<Idc>")[1].split( "</Idc>" )[0];

if (typeof(acc1) != 'undefined' && acc1 != '' && String(acc1) != '' && tempacc.indexOf('</Idd>') > -1) 
star1+= "\n\nBelow secound data\n\n" +  addd.split("<Idd>")[1].split( "</Idd>" )[0];

return star1;

But i am gettitng output as below while Idd of addd is blank:

 Hi,
 Details:
 Below first data:
 81
 Below secound data:

2 个答案:

答案 0 :(得分:0)

Not sure about your question, but you could check the length property of a string to check if it contains something:

var data = "";

if(data.length > 0){
    // Evaluates to true.
}

答案 1 :(得分:0)

if(typeof(myvariable) != 'undefined')
{
    // Variable is initialised i.e. data is set
}