如何用js判断null值

如题所述


我们在开发的时候经常会判断一个null值,那么我们该如何去判断呢?这篇文章就教大家如何用js判断null值的方法,分别用两种方法来对比一下如何正确用js判断null值。
以下是不正确的方法:
var exp=null;
if(exp==null){
alert("is null");
}exp 为 undefined 时,也会得到与 null 相同的结果,虽然 null 和 undefined 不一样。
注意:要同时判断 null 和 undefined 时可使用如上方法。
var exp=null;
if(!exp){
alert("is null");
}如果 exp 为 undefined,或数字零,或 false,也会得到与 null 相同的结果,虽然 null 和二者不一样。
注意:要同时判断 null、undefined、数字零、false 时可使用如上方法。
var exp=null;
if(typeof exp=="null"){
alert("is null");
}为了向下兼容,exp 为 null 时,typeof null 总返回 object,所以不能这样判断。
var exp=null;
if(isNull(exp)){
alert("is null");
}VBScript 中有 IsNull 这个函数,但 JavaScript 中没有。
以下是正确的方法:
var exp=null;
if(!exp && typeof exp!="undefined" && exp!=0){
alert("is null");
}typeof exp!="undefined" 排除了 undefined

exp!=0 排除了数字零和 false。
更简单的正确的方法:
var exp=null;
if(exp===null){
alert("is null");
}尽管如此,我们在 DOM 应用中,一般只需要用 (!exp) 来判断就可以了,因为 DOM 应用中,可能返回 null,可能返回 undefined,如果具体判断 null 还是 undefined 会使程序过于复杂。
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网