javascriptarraysundefined

Javascript array value is undefined ... how do I test for that


I am trying to test to see whether a Javascript variable is undefined.

You will see that I am not expecting the value of predQuery[preId] to be 'undefined' if I don't first get an alert saying "its unbelievable". But I often do, so I am guessing that my statement

 predQuery[preId]=='undefined') 

is not matching the undefined elements properly.

if((predQuery.length < preId) || (predQuery[preId]=="") || (predQuery[preId]=='undefined')){
   alert("its unbelievable");
   alert(predQuery[preId]);
   queryPreds[variables] = preId;
   queryObjs[variables] = objId;
   predQuery[preId] = variables;
}
else {
    alert(predQuery[preId]);
   var predIndex = predQuery[preId];
   queryPreds[predIndex] = preId;
   queryObjs[predIndex] = objId;
}

I can add more code if needed.


Solution

  • array[index] == 'undefined' compares the value of the array index to the string "undefined".
    You're probably looking for typeof array[index] == 'undefined', which compares the type.