Question: I'm trying to check if something is undefined in javascript (data[index]) and showing an alert if it is. However, it doesn't seem to be hitting.

I've tried:

```
  if(data[index]){ }

```

And i've tried:

```
  if (typeof(jsVar) == 'undefined') { }

```

However, neither of them seem to work and I get:

```
Uncaught ReferenceError: index is not defined 

```

Full code:

```
function myAlerts(data)
{
    $("#alertsListMissingPets").empty();
    $.mobile.loading( 'hide', { theme: "b", text: "Loading", textonly: false});
    if(data[index].Name){
    $.each(data, function(index) {
        console.log(data[index].LostDate)
        $("#alertsListMissingPets").append(" <li><a data-lostid='"+ data[index].LostKey + "' data-custom='"+ data[index].AnimalKey + "' href=\"#\">"+ data[index].Name + " <span class=\"ui-li-count\">"+ data[index].Distance + "</span></a></li>");
    });
    $("#alertsListMissingPets").listview('refresh');
    } else {
        function alertDismissed() {
            $.mobile.changePage("#mainpage");
        }
        navigator.notification.alert('No missing pets in your area!', alertDismissed, 'Good News', 'Okay' );
    }
}

```

Answer: 
Clearly, the error state that `index` is undefined. Take a look at your code :

```
if(data[index].Name){ //Check something with index here
$.each(data, function(index) { //Define index here

```

You can't use `index` outside the loop...
As stated in other answers/comments from the other posters, your use of $.each and the data[index] definition aren't correct. Try this (untested):

```
function myAlerts(data)
{
    $("#alertsListMissingPets").empty();
    $.mobile.loading( 'hide', { theme: "b", text: "Loading", textonly: false});
    if(data.length > 0) {
        $.each(data, function(value) { //index isn't needed based on your code
            $("#alertsListMissingPets").append(" <li><a data-lostid='"+ 
                value.LostKey + "' data-custom='"+ 
                value.AnimalKey + "' href=\"#\">"+ value.Name + 
                "<span class=\"ui-li-count\">"+ value.Distance + 
                "</span></a></li>");
        });
        $("#alertsListMissingPets").listview('refresh');
    } else {
        navigator.notification.alert('No missing pets in your area!', alertDismissed, 'Good News', 'Okay' );
    }
}

//moved this out of your else clause for aesthetics
function alertDismissed() {
    $.mobile.changePage("#mainpage");
}

```