zoukankan      html  css  js  c++  java
  • null undefined NaN

    数据类型
    6大基本
    Number  String Boolean
    Undefined Null.  Symbol (ES6)
     
    3大引用类型
    object array Function Regexp Date
     

    undefined和null有点类似,都表示"没有"的意思。

    然而类似不等于相同,下面就通过代码实例总结一下它们两者之间的区别。

    一.数据类型区别:

    undefined类型只有一个值,undefined。

    null类型只有一个值,null。

    也就是说undefined值和null值分别属于不同的数据类型,并且这两个数据类型都只有一个值。

     
     
    1.判断undefined: 说明:typeof 返回的是字符串,有六种可能: "number"、"string"、"boolean"、"object"、"function"、"undefined"
    .undefined通常是未初始化变量的值:
    /*
    1.判断undefined:
    说明:typeof 返回的是字符串,有六种可能:
    "number"、"string"、"boolean"、"object"、"function"、"undefined"
    */
    var tmp=undefined;
    if(typeof(tmp)=="undefined")
    alert("undefined");
     
    /*
    2.判断null:
    */
    .null通常用来标识一个空对象:    JavaScript中  null 的 typeof是object
    var tmp = null;
    if ( !tmp && typeof(tmp)!="undefined" && tmp!=0)
    alert("null");
     
     
    3.判断NaN: 说明:如果把 NaN 与任何值(包括其自身)相比得到的结果均是 false, 所以要判断某个值是否是 NaN,不能使用 == 或 === 运算符。
    /*
    3.判断NaN:
     
    var tmp = 0/0; if(isNaN(tmp)){ alert("NaN"); }
    说明:如果把 NaN 与任何值(包括其自身)相比得到的结果均是 false,
    所以要判断某个值是否是 NaN,不能使用 == 或 === 运算符。
     
    提示:isNaN() 函数通常用于检测 parseFloat() 和 parseInt() 的结果, 以判断它们表示的是否是合法的数字。当然也可以用 isNaN() 函数 来检测算数错误,比如用 0 作除数的情况。
     
     
     
    4.判断undefined和null:
    说明:null==undefined
    */
    var tmp = undefined;
    if (tmp== undefined)
    alert("null or undefined");
     
    var tmp = undefined;
    if (tmp== null)
    alert("null or undefined");
    }
     
     
    5.判断undefined、null与NaN:
    提示:一般不那么区分就使用这个足够。
     
    var tmp = null;
    if (!tmp)
    alert("null or undefined or NaN");
     
     
  • 相关阅读:
    HTB-靶机-Charon
    第一篇Active Directory疑难解答概述(1)
    Outlook Web App 客户端超时设置
    【Troubleshooting Case】Exchange Server 组件状态应用排错?
    【Troubleshooting Case】Unable to delete Exchange database?
    Exchange Server 2007的即将生命周期,您的计划是?
    "the hypervisor is not running" 故障
    Exchange 2016 体系结构
    USB PE
    10 months then free? 10个月,然后自由
  • 原文地址:https://www.cnblogs.com/iOS-mt/p/11077135.html
Copyright © 2011-2022 走看看