1.判断undefined:

  1. <span style="font-size: small;">var tmp = undefined;
  2. if (typeof(tmp) == "undefined"){
  3. alert("undefined");
  4. }</span>

说明:typeof 返回的是字符串,有六种可能:"number"、"string"、"boolean"、"object"、"function"、"undefined"

2.判断null:

  1. <span style="font-size: small;">var tmp = null;
  2. if (!tmp && typeof(tmp)!="undefined" && tmp!=0){
  3. alert("null");
  4. } </span>

3.判断NaN:

  1. <span style="font-size: small;">var tmp = 0/0;
  2. if(isNaN(tmp)){
  3. alert("NaN");
  4. }</span>

说明:如果把 NaN 与任何值(包括其自身)相比得到的结果均是 false,所以要判断某个值是否是 NaN,不能使用 == 或 === 运算符。

提示:isNaN() 函数通常用于检测 parseFloat() 和 parseInt() 的结果,以判断它们表示的是否是合法的数字。当然也可以用 isNaN() 函数来检测算数错误,比如用 0 作除数的情况。

4.判断undefined和null:

  1. <span style="font-size: small;">var tmp = undefined;
  2. if (tmp== undefined)
  3. {
  4. alert("null or undefined");
  5. } </span>
  1. <span style="font-size: small;">var tmp = undefined;
  2. if (tmp== null)
  3. {
  4. alert("null or undefined");
  5. }</span>

说明:null==undefined

<!--EndFragment-->

5.判断undefined、null与NaN:

  1. <span style="font-size: small;">var tmp = null;
  2. if (!tmp)
  3. {
  4. alert("null or undefined or NaN");
  5. }</span>

提示:一般不那么区分就使用这个足够。

JS中如何判断null、undefined与NaN的更多相关文章

  1. 【转发】JS中如何判断null/ undefined/IsNull

    以下是不正确的方法:var exp = null;if (exp == null){ alert("is null");}exp 为 undefined 时,也会得到与 null ...

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

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

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

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

  4. JS中如何判断null

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

  5. JS 中如何判断 undefined 和 null

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

  6. json,js中typeof用法详细介绍及NaN、 null 及 undefined 的区别

    JSON.parse(jsonstr); //可以将json字符串转换成json对象 JSON.stringify(jsonobj); //可以将json对象转换成json对符串 在js使用中的一个函 ...

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

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

  8. 简写代码:当变量为false时['',false,null,undefined,0,NaN]时,返回默认值

    当变量为'',false,null,undefined,0,NaN时,返回默认值 var a='' a || 'hello world'   "hello world" var a ...

  9. JS中原始类型Null和Undefined

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

随机推荐

  1. [转载]hao123军事频道首页JQ焦点图

    适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗. *本作品由[站长素材]收集整理,转载请注明出处! 下载地址

  2. Strlen()与sizeof()

    在学习C语言时以及面试中,经常会见到strlen()与sizeof()这一对容易混淆的概念,搞清楚这两个概念,往往考察了编程人员对语言的基本掌握能力. 首先大家先明确两个概念是: 1.strlen() ...

  3. 使用Phalcon开发工具碰到的数据库问题"Table 'XXX' doesn't exist in database when dumping meta-data for XXX"

    使用Phalcon开发工具,通过命令行生成程序框架 设置好config.php,在对数据库进行读取.保存数据的时候出现了问题“Table 'XXX' doesn't exist in database ...

  4. 基于jquery框架的ajax搜索显示

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  5. YouTrack Changing Database Location for EXE Distribution(windows service)

    If you have installed YouTrack from EXE Distribution, then the best way to change the database locat ...

  6. Ant编译环境

    使用ant编译有关于C compiler(cc task)的时候,需要确认vs的环境有无配置, 否则会遇到形形色色的问题,比如各种 vs编译工具找不到的错误, 一个比较简单的解决方案,就是在执行ant ...

  7. Scala下载安装配置(Mac)

    ---恢复内容开始--- 1.访问scala的官网这里下载最新版的scala. 2.解压缩文件包,可将其移动至/usr/local/share下 1 mv /download/scalapath /u ...

  8. library cache lock和cursor: pin S wait on X等待

    1.现象: 客户10.2.0.4 RAC环境,出现大量的library cache lock和cursor: pin S wait on X等待,经分析是由于统计信息收集僵死导致的.数据库在8点到9点 ...

  9. 我常用的Linux命令

    CD: ..        —-切换到上层目录 ~        —-回到家目录(/home/你的登录名/) LS: -a        —-显示指定目录所有文件,包括文件名以 . 开头的文件 -l  ...

  10. 动软Model 模板 生成可空类型字段

    动软代码 生成可空类型 <#@ template language="c#" HostSpecific="True" #> <#@ outpu ...