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 会使程序过于复杂。
JS中如何判断null的更多相关文章
- 160304-02、JS 中如何判断null 和undefined
JavaScript 中有两个特殊数据类型:undefined 和 null,下节介绍了 null 的判断,下面谈谈 undefined 的判断. 以下是不正确的用法: var exp = undef ...
- 【转发】JS中如何判断null/ undefined/IsNull
以下是不正确的方法:var exp = null;if (exp == null){ alert("is null");}exp 为 undefined 时,也会得到与 null ...
- JS中如何判断null、undefined与NaN
1.判断undefined: <span style="font-size: small;">var tmp = undefined; if (typeof(tmp) ...
- JS 中如何判断 undefined 和 null
JS 中如何判断 undefined JavaScript 中有两个特殊数据类型:undefined 和 null,下节介绍了 null 的判断,下面谈谈 undefined 的判断. 以下是不正确的 ...
- JS中如何判断对象是对象还是数组
JS中如何判断对象是对象还是数组 一.总结 一句话总结:typeof Array.isArray === "function",Array.isArray(value)和Objec ...
- js中NAN、NULL、undefined的区别
NaN:保留字(表明数据类型不是数字) undefined:对象属性或方法不存在,或声明了变量但从未赋值.即当你使用了对象未定的属性或者未定义的方法时或当你声明一个变量,但你确从未对其进行赋值,便对其 ...
- Js中数据类型判断的几种方法
判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...
- js中使用0 “” null undefined {}需要注意
注意:在js中0为空(false) ,代表空的还有“”,null ,undefined: 如果做判断if(!上面的四种值):返回均为false console.log(!null);// true c ...
- 区分JS中的undefined,null,"",0和false
在程序语言中定义的各种各样的数据类型中,我们都会为其定义一个"空值"或"假值",比如对象类型的空值null,.NET Framework中数据库 字段的空值DB ...
随机推荐
- STOMP协议规范--转载
原文地址:http://simlegate.com/2013/10/17/stomp-specification-1.2/ 摘要 STOMP是一个简单的可互操作的协议, 被用于通过中间服务器在客户端之 ...
- X - Urban Elevations
Urban Elevations An elevation of a collection of buildings is an orthogonal projection of the buil ...
- The Romantic Hero
Problem Description There is an old country and the king fell in love with a devil. The devil always ...
- this的分析
this是js中的一个关键字,当函数运行时,会自动生成的一个对象,只能在函数内部使用.随着函使用场合不同,this值会变化,但是始终指向调用函数的那个对象. 1.纯粹的函数调用 function bo ...
- Sqlite和CoreData的区别
使用方便性.实际上,一个成熟的工程中一定是对数据持久化进行了封装的,因此底层使用的到底是core data还是sqlite,不应该被业务逻辑开发者关心.因此,即使习惯写SQL查询的人,也应该避免在业务 ...
- use selenium in scrapy webdriver
https://pypi.python.org/pypi/selenium from selenium import webdriver from selenium.webdriver.common. ...
- [Android]Log打印
package com.lurencun.android.system; import android.util.Log; public class ExLog { static final Stri ...
- JS中获取table节点的tr或td的内容
<table id="tb1" width="200" border="1" cellpadding="4" ce ...
- 如何在Java中定义常量(Constant)
原本引自 http://blog.csdn.net/autofei/article/details/6419460 /** * Method One */ interface ConstantInt ...
- ImageView的src和background的区别
参考资料: http://blog.csdn.net/dalleny/article/details/14048375 http://www.android100.org/html/201508/27 ...