Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working with titanium ,

my code looks like as ,

var currentData = new Array();

if(currentData[index]!==""||currentData[index]!==null||currentData[index]!=='null')
{
    Ti.API.info("is exists  " + currentData[index]);
    return true;
}
else
{   
    return false;
}

I am passing index to array currentData,

For non existing element , i am still not able to detect it using above code

share|improve this question
1  
Your logic is wrong. You need conjunctions (&&) between the individual conditions. –  Jan Kuča Oct 28 '12 at 9:59

5 Answers 5

up vote 48 down vote accepted

Use typeof arrayName[index] === 'undefined'

i.e.

if(typeof arrayName[index] === 'undefined') {
    // does not exist
}
else {
    // does exist
}
share|improve this answer
3  
+1, nice. You can also use if(arrayName[index] === 'undefined') as a shortcut –  AnchovyLegend Oct 8 '13 at 0:08
12  
@AnchovyLegend no, you cannot! But you can use if(arrayName[index] === undefined). –  Denis V Nov 29 '13 at 15:01
4  
this fails, if the item is there, but it's value is undefined; use this answer instead -> stackoverflow.com/questions/1098040/… –  Matus May 17 '14 at 21:25
    
as @Matus said, there is more explanation here, you should be aware on. –  S.Thiongane Aug 8 '14 at 8:47

I had to wrap techfoobar's answer in a try..catch block, like so:

try {
  if(typeof arrayName[index] == 'undefined') {
    // does not exist
  }
  else {
  // does exist
  }
} 
catch (error){ /* ignore */ }

...that's how it worked in chrome, anyway (otherwise, the code stopped with an error).

share|improve this answer
var myArray= ["Banana", "Orange", "Apple", "Mango"];

if(myArray.indexOf(searchTerm)==-1)
  alert("element doesn't exist");
else
  alert("element found");
share|improve this answer
1  
Unfortunately, this one doesn't work in IE 7 and below. –  darksoulsong Oct 7 '14 at 15:21

If you use underscore.js then these type of null and undefined check are hidden by the library.

So your code will look like this -

var currentData = new Array();

if (_.isEmpty(currentData)) return false;

Ti.API.info("is exists  " + currentData[index]);

return true;

It looks much more readable now.

share|improve this answer
    
Even if your answer is right, I would just think twice for this. Your code would become underscore.js dependent only for checking an empty value. Just do a simple wrapper function isset(v) { return (typeof v !== 'undefined'); } –  Heroselohim Nov 26 '14 at 14:55

If elements of array are also simple objects or arrays, you can use some function:

// search object
var element = { item:'book', title:'javasrcipt'};

[{ item:'handbook', title:'c++'}, { item:'book', title:'javasrcipt'}].some(function(el){
    if( el.item === element.item && el.title === element.title ){
        return true; 
     } 
});

[['handbook', 'c++'], ['book', 'javasrcipt']].some(function(el){
    if(el[0] == element.item && el[1] == element.title){
        return true;
    }
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.