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. python练习程序(c100经典例17)

    题目: 输入一行字符,分别统计出其中英文字母.空格.数字和其它字符的个数. def foo(a): l=len(a); letters=0; space=0; digit=0; others=0; f ...

  2. 【C#学习笔记】浏览目录得到路径

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. UVa11582 Colossal Fibonacci Numbers!

    #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> us ...

  4. 动态加载 移除js file

    动态加载.移除.替换js/css文件 stylesheetjavascriptcssfunctionnull <script language="javascript"> ...

  5. 备忘录 - numpy基本方法总结

    一.数组方法 创建数组:arange()创建一维数组:array()创建一维或多维数组,其参数是类似于数组的对象,如列表等 反过来转换则可以使用numpy.ndarray.tolist()函数,如a. ...

  6. 集合框架null与size=0

    被QA人员一眼指出来的问题,唉,好丢人 上栗子

  7. 【转】iOS 开发之协议protocal-代理传值delegate

    原文网址:http://www.cnblogs.com/wzrong/p/3201938.html 刚开始做iOS开发的时候,对 protocol.delegate 的理解一直都是晕晕乎乎一知半解的状 ...

  8. 使用crs_setperm修改RAC资源的所有者及权限

    Oracle RAC 集群中,对于各种资源的管理,也存在所有者与权限的问题.crs_getperm与crs_setperm则是这样的一对命令,主要用于查看与修改集群中resource的owner,gr ...

  9. nodejs模拟http-post文件的方法示例

    var fs = require("fs"); var http = require('http'); function PostFileToServer(sFileName, d ...

  10. 3500常用汉字与标点符号(已排除不支持GB2312的)

    .?!,.::“”‘’…()<>〈〉[].,:;!?-'_"'()[]<>|&~;+-*/=<>0123456789ABCEFGHIJKLMNOP ...