没有合适的资源?快使用搜索试试~ 我知道了~
本文实例讲述了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 相同的结
资源推荐
资源详情
资源评论
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 会使程序过于复杂。
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript中json操作技巧总结》、《JavaScript切换特效与技
巧总结》、《JavaScript查找算法技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、
《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。
资源评论
weixin_38518885
- 粉丝: 8
- 资源: 942
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功