160304-02、JS 中如何判断null 和undefined
JavaScript 中有两个特殊数据类型:undefined 和 null,下节介绍了 null 的判断,下面谈谈 undefined 的判断。
以下是不正确的用法:
var exp = undefined;
if (exp == undefined)
{
alert("undefined");
}
exp 为 null 时,也会得到与 undefined 相同的结果,虽然 null 和 undefined 不一样。注意:要同时判断 undefined 和 null 时可使用本法。
var exp = undefined;
if (typeof(exp) == undefined)
{
alert("undefined");
}
typeof 返回的是字符串,有六种可能:"number"、"string"、"boolean"、"object"、"function"、"undefined"
以下是正确的用法:
var exp = undefined;
if (typeof(exp) == "undefined")
{
alert("undefined");
}
--------------------------------------------------------------------------------
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 或者数字零,也会得到与 null 相同的结果,虽然 null 和二者不一样。注意:要同时判断 null、undefined 和数字零时可使用本法。
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 会使程序过于复杂。
160304-02、JS 中如何判断null 和undefined的更多相关文章
- JS中如何判断null、undefined与NaN
1.判断undefined: <span style="font-size: small;">var tmp = undefined; if (typeof(tmp) ...
- JS中如何判断null
var exp = null; if (exp == null) { alert("is null"); } exp 为 undefined 时,也会得到与 null 相同的结果, ...
- 【转发】JS中如何判断null/ undefined/IsNull
以下是不正确的方法:var exp = null;if (exp == null){ alert("is null");}exp 为 undefined 时,也会得到与 null ...
- js中NAN、NULL、undefined的区别
NaN:保留字(表明数据类型不是数字) undefined:对象属性或方法不存在,或声明了变量但从未赋值.即当你使用了对象未定的属性或者未定义的方法时或当你声明一个变量,但你确从未对其进行赋值,便对其 ...
- JS中原始类型Null和Undefined
Undefined类型只有一个值,即undefined.当声明的变量还未被初始化时,变量的默认值为undefined.Null类型也只有一个值,即null.null用来表示尚未存在的对象,常用来表示函 ...
- JS 中如何判断 undefined 和 null
JS 中如何判断 undefined JavaScript 中有两个特殊数据类型:undefined 和 null,下节介绍了 null 的判断,下面谈谈 undefined 的判断. 以下是不正确的 ...
- JS中如何判断对象是对象还是数组
JS中如何判断对象是对象还是数组 一.总结 一句话总结:typeof Array.isArray === "function",Array.isArray(value)和Objec ...
- JS中判断null、undefined与NaN的方法
写了个 str ="s"++; 然后出现Nan,找了一会. 收集资料如下判断: 1.判断undefined: 代码如下: <span style="font-siz ...
- Js中数据类型判断的几种方法
判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...
随机推荐
- SQLSERVER中的timestamp 和 C#中的byte[] 转换
项目中由于需求设计,数据库中需要一个timestamp时间戳类型的字段来作为区别数据添加和修改的标识.由于timestamp在SQL SERVER 2005数据库中,不可为空的timestamp类型在 ...
- atitit.判断时间重叠方法总结 java c++ c#.net js php
atitit.判断时间重叠方法总结 java c++ c#.net js php 1. 判断时间重叠具体流程思路 1 2. 重叠算法 实际上就是日期集合跟个时间集合的的交集(乘法算法) 1 3. 代 ...
- CBiontCache
/************************************************************************/ /* 预先加载一些生物以备将来使用 */ /* 专 ...
- 用wget做站点镜像
用wget做站点镜像 -- :: 分类: LINUX # wget -r -p -np -k http://xxx.edu.cn -r 表示递归下载,会下载所有的链接,不过要注意的是,不要单独使用这个 ...
- 每日英语:Who Ruined The Humanities?
You've probably heard the baleful reports. The number of college students majoring in the humanities ...
- linux命令(6)crontab的用法和解析,修改编辑器
注意: 如果不是vim打开的,可以先: crontab -e 命令将检查环境变量$ EDITOR和$ VISUAL以覆盖默认文本编辑器,所以... export VISUAL=vim or expor ...
- ajax请求数据动态渲染表格
$.ajax({ url: "/flow/userTaskFileShow.cc", data: {"processDefinitionId": pdid, & ...
- 设置open_cursors参数
1.进入终端,输入命令:sqlplus /nolog 2.输入命令:conn /as sysdba 3.输入命令:alter system set open_cursors=1000 scope=me ...
- ftok函数例子
#include <stdio.h>#include <sys/types.h>#include <sys/ipc.h>int main( void ){ int ...
- curses库--libncurses5-dev--游标移动及屏幕的显示
curses是一个在Linux/Unix下广泛应用的图形函数库.,作用是可以绘制在DOS下的用户界面和漂亮的图形. curses的名字起源于"cursor optimization" ...