javascript 判断变量是否为null

我的方法:java

if(String(test) == "null")函数

有错,请你们指出,谢谢。blog

 

如下是网上的一些资料:ip

http://dmewy.javaeye.com/blog/245300get

如下是不正确的用法:test

var exp = null;
if (exp == null)
{
    alert("is null");
}object

exp 为 undefined 时,也会获得与 null 相同的结果,虽然 null 和 undefined 不同。程序

var exp = null;
if (!exp)
{
    alert("is null");
}方法

若是 exp 为 undefined 或者数字零,也会获得与 null 相同的结果,虽然 null 和两者不同。兼容

var exp = null;
if (typeof(exp) == "null")
{
    alert("is null");
}

为了向下兼容,exp 为 null 时,typeof 总返回 object。

var exp = null;
if (isNull(exp))
{
    alert("is null");
}

JavaScript 中没有 isNull 这个函数。

 


 

如下是正确的用法:

var exp = null;
if (!exp && typeof(exp)!="undefined" && exp!=0)
{
    alert("is null");
} 


尽管如此,咱们在DOM应用中,通常只须要用 (!exp) 来判断就能够了,由于 DOM 应用中,可能返回 null,可能返回 undefined,若是具体判断 null 仍是 undefined 会使程序过于复杂。

------------------------------------------------------------------------

相关文章
相关标签/搜索