以下是不正确的方法:
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 会使程序过于复杂。

直接(exp)或者(!exp)就行了,没有必要单一判断。

【转发】JS中如何判断null/ undefined/IsNull的更多相关文章

  1. 160304-02、JS 中如何判断null 和undefined

    JavaScript 中有两个特殊数据类型:undefined 和 null,下节介绍了 null 的判断,下面谈谈 undefined 的判断. 以下是不正确的用法: var exp = undef ...

  2. JS中如何判断null、undefined与NaN

    1.判断undefined: <span style="font-size: small;">var tmp = undefined; if (typeof(tmp)  ...

  3. JS中如何判断null

    var exp = null; if (exp == null) { alert("is null"); } exp 为 undefined 时,也会得到与 null 相同的结果, ...

  4. js中使用0 “” null undefined {}需要注意

    注意:在js中0为空(false) ,代表空的还有“”,null ,undefined: 如果做判断if(!上面的四种值):返回均为false console.log(!null);// true c ...

  5. JS 中如何判断 undefined 和 null

    JS 中如何判断 undefined JavaScript 中有两个特殊数据类型:undefined 和 null,下节介绍了 null 的判断,下面谈谈 undefined 的判断. 以下是不正确的 ...

  6. js中NAN、NULL、undefined的区别

    NaN:保留字(表明数据类型不是数字) undefined:对象属性或方法不存在,或声明了变量但从未赋值.即当你使用了对象未定的属性或者未定义的方法时或当你声明一个变量,但你确从未对其进行赋值,便对其 ...

  7. JS中如何判断对象是对象还是数组

    JS中如何判断对象是对象还是数组 一.总结 一句话总结:typeof Array.isArray === "function",Array.isArray(value)和Objec ...

  8. JS中原始类型Null和Undefined

    Undefined类型只有一个值,即undefined.当声明的变量还未被初始化时,变量的默认值为undefined.Null类型也只有一个值,即null.null用来表示尚未存在的对象,常用来表示函 ...

  9. JS去除对象或数组中的空值('',null,undefined,[],{})

    javascript去掉对象或数组中的'',null,undefined,[],{}.思路就是创建一个新的空对象,然后对传入的对象进行遍历,只把符合条件的属性返回,保留有效值,然后就相当于把空值去掉了 ...

随机推荐

  1. Linux gcc编译参数

    最近编译一份开源代码,一编译就直接报错.我看了下报错信息,有点诧异.这些信息,放平常顶多就是个warnning而已啊,他这里怎么变成了error呢?我看了下Makefile,发现编译参数多了个-Wer ...

  2. WaitForSingleObject和CEvent用法

    WaitForSingleObject函数用来检测hHandle事件的信号状态,当函数的执行时间超过dwMilliseconds就返回,但如果参数dwMilliseconds为INFINITE时函数将 ...

  3. HTTP 协议中 Vary 的一些研究

    经常抓包看 HTTP 请求的同学应该对 Vary 这个响应头字段并不陌生,它有什么用?用 PageSpeed 工具检查页面时,经常看到「Specify a Vary: Accept-Encoding ...

  4. dwz tabs table实现翻页及各tabs查询

    效果如图:

  5. 《逆袭大学:传给IT学子的正能量》

    <逆袭大学:传给IT学子的正能量> 基本信息 作者: 贺利坚 丛书名: 图灵原创 出版社:人民邮电出版社 ISBN:9787115347473 上架时间:2014-3-3 出版日期:201 ...

  6. 用户组篇endgrent getpw getuid

    endgrent(关闭组文件) 相关函数 getgrent,setgrent 表头文件 #include<grp.h> #include<sys/types.h> 定义函数 v ...

  7. CentOS 5.5 虚拟机安装 VirtualBox 客户端增强功能

    .启动安装在 VirtualBox 中的 CentOS 5.5 虚拟机,点击“设备” => “安装增强功能”.这个时候你就可以看到有一个“光盘”已经挂载到 CentOS 5.5 的桌面上了.它包 ...

  8. 【ContestHunter】【弱省胡策】【Round5】

    反演+FFT+构造+DP 写了这么多tag,其实我一个也不会 A 第一题是反演……数据范围10W,看着就有种要用FFT等神奇算法的感觉……然而蒟蒻并不会推公式,只好写了20+10分的暴力,然而特判30 ...

  9. 混沌数学之拉比诺维奇-法布里康特方程(Rabinovich-Fabrikant equations)

    拉比诺维奇-法布里康特方程(Rabinovich-Fabrikant equations)是 1979年苏联物理学家拉比诺维奇和法布里康特提出模拟非平衡介 质自激波动的非线性常微分方程组: dot{x ...

  10. Simplify Path leetcode java

    题目: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/&qu ...