Search code examples
javascripttitaniumtitanium-mobile

How to check if an array index exists or not in JavaScript?


I am working with Titanium, my code looks like this:

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 an index to the currentData array. I still can't detect a non-existing index using the above code.


Solution

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

    i.e.

    if(typeof arrayName[index] === 'undefined') {
        // does not exist
    }
    else {
        // does exist
    }