tags:

views:

31

answers:

3

Hi all,

I am doing the following null check , even thought the object is null its still unable to check that

if(callbackResults.Details[0].Tags!='') // Tags are coming as null from backend..

details is a json object. Tags is again a object array inside details.

the above null check is failing and its going inside the loop

can you tell whats going wrong there.

+3  A: 

You can't test for null by comparison with an empty string. Try this:

 if (callbackResults.Details[0].Tags) {
   // not null
 }
meagar
Perfect it worked.
gov
+3  A: 

That is a check to see if Tags is an empty string. A check for 'not null' in JavaScript is this:

if(callbackResults.Details[0].Tags)

Note that this also checks for a booean 'true' condition, but this is the common way to make sure something is not null in javascript.

Mike Webb
Technically this is a check for null or any other value that evaluates to `false` when converted to a boolean.
meagar
@meagar - Good catch. I added this to the answer, although it's the other way around.
Mike Webb
A: 

Changing the if should do it, as described above. Else, if Tags is an array, you might check if the length is larger than zero:

if(callbackResults.Details[0].Tags.length > 0)
Jacco