Javascript数组值未定义...我该如何测试 [英] Javascript array value is undefined ... how do I test for that

查看:84
本文介绍了Javascript数组值未定义...我该如何测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试是否未定义Javascript变量。

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

您将看到我不期望predQuery [preId]的值为'未定义'如果我没有首先得到警告说它令人难以置信。但我经常这样做,所以我猜我的陈述

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.

推荐答案

array [index] =='undefined'比较字符串undefined的数组索引。

你可能正在寻找 typeof array [index] =='undefined',它比较了类型

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.

这篇关于Javascript数组值未定义...我该如何测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆