您的位置:首页 > Web前端 > JavaScript

js中(!exp)

2015-11-12 23:20 531 查看
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 会使程序过于复杂。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: