`
deejay
  • 浏览: 142615 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

理解js中的:Null、undefined、""、0、false

阅读更多
理解js中的:Null、undefined、""、0、false
2010-01-02 05:54

1、undefined、null、""、0、false这五个值在if语句中做判断,都会执行false分支
2、    undefined和null比较特殊,
    虽然null的类型是object,但是null不具有任何对象的特性,
    就是说我们并不能执行null.toString()、null.constructor等对象实例的默认调用。
    所以从这个意义上来说,null和undefined有最大的相似性。
    ★★看看null == undefined的结果(true)也就更加能说明这点。
    不过相似归相似,还是有区别的,
    就是和数字运算时,10 + null结果为:10;10 + undefined结果为:NaN。
3.""、0和false虽然在if语句表现为"假值",可它们都是有意义数据,只是被作为了"空值"或"假值"
因为:★★"".toString(),(0).toString()和false.toString()都是合法的可执行表达式。

4.当尝试读取不存在的对象属性时也会返回 undefined。
提示:只能用 === 运算来测试某个值是否是未定义的,因为 == 运算符认为 undefined 值等价于 null。
注释:null 表示无值,而 undefined 表示一个未声明的变量,或已声明但没有赋值的变量,或一个并不存在的对象属性。

 

<html>
<head>
    <TITLE>解决Null 和 undefined 等问题</TITLE>
    <script type="text/javascript">
    
    var str="How are you doing today?"
    
    document.write(str.split(" ") + "<br />")
    document.write(str.split("") + "<br />")
    document.write(str.split(" ",3))
    
/**
    * 这里有一题目:JS中,如何判断一个对象的值是不是NULL?
    *
        解:
        if(!obj||obj=='null'||typeof(object)=="undefined"))   
          {   
                    alert('NULL');   
            
          }   
          
     
     
     
     
    //=============================================================================
    //各种类型
    //_____________________________________________________________________________
    document.write("<br>");
    document.write("各种类型:");
    document.write("<br>");
    if(null == undefined){document.write("<br><br> null == undefined 为ture<br>")}
    if(typeof(undefined) == 'undefined')document.write("typeof(undefined) == 'undefined'为true<br>");
    if(typeof(null) == 'object'){document.write("typeof(null) == 'object'为ture<br>")}
    if(typeof("") == 'string')document.write("typeof(\"\") == 'string'为true<br>")
    if(typeof(0) == 'number'){document.write("typeof(0) == 'number'为true<br>")}
    if(typeof(false) == 'boolean'){document.write("typeof(false) == 'boolean'为true<br><br>")}
    /*
    以上段运行结果:
    null == undefined 为ture
    typeof(undefined) == 'undefined'为true
    typeof(null) == 'object'为ture
    typeof("") == 'string'为true
    typeof(0) == 'number'为true
    typeof(false) == 'boolean'为true
    */


    //===============================================
    //测试何时if(判断条件为false)
    //______________________________________________
    document.write("<br>");
    document.write("测试何时if(判断条件为false)");
    document.write("<br>");
    if(null){document.write("if(null)<br>")}
    if(undefined){document.write("if(undefined)<br>")}
    if(""){document.write('if("")<br>')}
    if("123"){document.write('if("123")<br>')}
    if(0){document.write('if(0)<br>')}
    if(1){document.write("if(1)<br>")}
    if(true){document.write("if(true)<br>")}
    if(false){document.write('f(false)<br>')}
    // if(){}
    /*
    以上段运行结果:
    if("123")
    if(1)
    if(true)
    
    结论:
    ★★★★★★undefined、null、""、0、false这五个值在if语句中做判断,都会执行false分支
    */

    //=======================================================
    //undefined和null与“算数”运算符
    //_______________________________________________________
    
    document.write("<br>");
    document.write("undefined和null与“算数”运算符");
    document.write("<br>");
    document.write(10 + null+"<br>");
    document.write(10 + undefined);
    document.write("<br>");
    /*
    以上段运行结果:
    10
    NaN
    
    undefined和null比较特殊,
    虽然null的类型是object,但是null不具有任何对象的特性,
    就是说我们并不能执行null.toString()、null.constructor等对象实例的默认调用。
    所以从这个意义上来说,null和undefined有最大的相似性。
    ★★看看null == undefined的结果(true)也就更加能说明这点。
    不过相似归相似,还是有区别的,
    就是和数字运算时,10 + null结果为:10;10 + undefined结果为:NaN。
    */

    //=====================================================================================
    //""、0和false虽然在if语句表现为"假值",可它们都是有意义数据,只是被作为了"空值"或"假值",
    //___________________________________________________________________________________
    document.write('""、0和false虽然在if语句表现为"假值",可它们都是有意义数据,只是被作为了"空值"或"假值"'); document.write("<br>");
    document.write('"".toString():' + "".toString());   document.write("<br>");
    document.write("(0).toString():" + (0).toString()); document.write("<br>");
    document.write("false.toString():" + false.toString()); document.write("<br>");

    /*
    以上段运行结果:    
    0
    false
    
    结论:
    ""、0和false虽然在if语句表现为"假值",可它们都是有意义数据,
    只是被作为了"空值"或"假值",
    因为:★★"".toString(),(0).toString()和false.toString()都是合法的可执行表达式。
    */

 

    //=======================================================
    //undefined、null、""、0、false这五个值转换为String时的差异
    //_______________________________________________________
    document.write("<br>");
    document.write('undefined、null、""、0、false这五个值转换为String时的差异');document.write("<br>");
    document.write("String(undefined):" + String(undefined));   document.write("<br>");
    document.write("String(null):" + String(null)); document.write("<br>");
    document.write('String(""):' + String("")); document.write("<br>");
    document.write("String(0):" + String(0));   document.write("<br>");
    document.write("String(false):" + String(false) ); document.write("<br>");

    //==========================================
    // 测试,当某成员并未定义,而直接使用时:此时是否为undefined,以及相关执行结果。
    //==========================================
/**
        参考:http://www.w3school.com.cn/js/jsref_undefined.asp
        定义和用法
                undefined 属性用于存放 JavaScript 的 undefined 值。
        
        语法
                undefined说明
                无法使用 for/in 循环来枚举 undefined 属性,也不能用 delete 运算符来删除它。
        
        undefined 不是常量,可以把它设置为其他值。
        
        ★★★★当尝试读取不存在的对象属性时也会返回 undefined。
        提示和注释
                ★★★★提示:只能用 === 运算来测试某个值是否是未定义的,因为 == 运算符认为 undefined 值等价于 null。
                ★★★★注释:null 表示无值,而 undefined 表示一个未声明的变量,或已声明但没有赋值的变量,或一个并不存在的对象属性。
    */
    document.write("<br>");
    document.write('测试,当某成员并未定义,而直接使用时:此时是否为undefined,以及相关执行结果。');
    document.write("<br>");

//这里的"abcd"并没有事先定义
/*
    if(abcd){document.write("if(abcd)");}
    if(!abcd){document.write("if(!abcd)");}
    if(abcd == undefined){document.write("abcd == undefined");}
    if(abcd == 'undefined'){document.write("abcd == 'undefined'");}
注:以上4行,均没有相应信息显示!!!!!!!!!!!!!!
只有下边这行显示:typeof(abcd) == 'undefined'为true
    if(typeof(abcd) == 'undefined'){document.write("typeof(abcd) == 'undefined'为true<br>");}
    
    当然如果用alert(abcd); 的话仍然没有反应,不会alert出类似"undefined"的信息
*/
    if(typeof(abcd) == 'undefined'){document.write("typeof(abcd) == 'undefined'为true<br>");}

    var aa;
    document.write("aa:" + aa +"<br>");
    if(aa == undefined){document.write("aa == undefined为true<br>");}
    if(typeof(aa) == 'undefined'){document.write("typeof(aa) == 'undefined'为true<br>");}
    if(aa == null){document.write("aa == null 为true<br>");}
    if(aa){document.write("if(aa)<br>");}
    if(!aa){document.write("if(!aa)<br><br>");}


    var t1="";
    var t2;
    if (t1===undefined) {document.write("t1 is undefined<br>");}
    if (t2===undefined) {document.write("t2 is undefined<br>");}

    </script>
    <LINK REL=STYLESHEET TYPE="text/css" HREF="resource/contract_htqd.css">
</head>
<body>

</body>
</html>

运行结果:

How,are,you,doing,today?
H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
How,are,you
各种类型:


null == undefined 为ture
typeof(undefined) == 'undefined'为true
typeof(null) == 'object'为ture
typeof("") == 'string'为true
typeof(0) == 'number'为true
typeof(false) == 'boolean'为true


测试何时if(判断条件为false)
if("123")
if(1)
if(true)

undefined和null与“算数”运算符
10
NaN
""、0和false虽然在if语句表现为"假值",可它们都是有意义数据,只是被作为了"空值"或"假值"
"".toString():
(0).toString():0
false.toString():false

undefined、null、""、0、false这五个值转换为String时的差异
String(undefined):undefined
String(null):null
String(""):
String(0):0
String(false):false

测试,当某成员并未定义,而直接使用时:此时是否为undefined,以及相关执行结果。
typeof(abcd) == 'undefined'为true
aa:undefined
aa == undefined为true
typeof(aa) == 'undefined'为true
aa == null 为true
if(!aa)

t2 is undefined

分享到:
评论

相关推荐

    区分JS中的undefined,null,&quot;&quot;,0和false

    区分JS中的undefined,null,"",0和false

    区分JS中的undefined,null,,0和false

    在程序语言中定义的各种各样的数据类型中,我们都会为其定义一个”空值”或”假值”,比如对象类型的空值null,.NET Framework中数据库字段的空值DBNull,boolean类型的假值false等等。在JavaScript中也有很多种的...

    JavaScript中undefined和null的区别

    JavaScript中undefined和null的区别 JavaScript两个表示”无”的值:undefined和null。我在平时只是null用的多一点,undefined只是在报错中经常遇到。下面针对这两个数据类型的异同做一下详细的比较。 1.undefined和...

    有关JS中的0,null,undefined,[],{},””””,false之间的关系

    0与false 0==false true 0与”: 0==” true  0与[]: 0==[] true  0与NaN: 0==NaN false 0与undefined 0==undefined false 0与null 0==null false  0与{} 0=={} false ”空值与一些值的比较 '' == false ...

    Javascript中的false、0、null、undefined和空字符串对象[归类].pdf

    Javascript中的false、0、null、undefined和空字符串对象[归类].pdf

    JavaScript中的null和undefined用法解析

    null和undefined属于js中两种不同的基本数据类型,都可以表示“没有”,含义非常相似。将一个变量赋值为undefined或null,老实说,语法效果几乎没区别。并且在if语句的判断条件中,它们都会自动转为false,相等...

    JS中判断null的方法分析

    本文实例讲述了JS中判断null的方法。分享给大家供大家参考,具体如下: 以下是不正确的方法: var exp = null; if (exp == null) { alert&#40;is null&#41;; } exp 为 undefined 时,也会得到与 null 相同的结果...

    JavaScript undefined及null区别实例解析

    在JavaScript中,将一个变量赋值为undefined或null,老实说,几乎没区别。 var a = undefined; var a = null; 上面代码中,a变量分别被赋值为undefined和null,这两种写法几乎等价。 undefined和null在if语句中,...

    详解JavaScript中undefined与null的区别

    一、相似性在JavaScript中,将一个变量赋值为undefined或null,老实说,几乎没区别。 代码如下:var a = undefined;var a = null; 上面代码中,a变量分别被赋值为undefined和null,这两种写法几乎等价。undefined和...

    javascript中的undefined 与 null 的区别 补充篇

    之前软件开发网发不过类似的文章JavaScript null和undefined区别分析JavaScript Undefined,Null类型和NaN值区别先说说undefined: Javascript中的变量是弱类型的(关于这个我想就不用我多解释啦), 所以声明变量的...

    javascript中undefined与null的区别

    大多数计算机语言,有且仅有一个表示”无”的值,比如,C语言的NULL,Java语言的null,Python语言的None,Ruby语言的nil。...undefined和null在if语句中,都会被自动转为false,相等运算符甚至直接

    老生常谈javascript中逻辑运算符&&和||的返回值问题

    NaN null undefined 0 false; 所以3||5返回的是3; 如果||左右两边都是以上类型的值时,会返回最后一个 如 var a=0||null||undefined则a返回的值是undefined; 其次是&&的返回值问题: &&的返回值会返回最早遇到以下...

    有关JS中的0,null,undefined,[],{},'''''''',false之间的关系

    主要介绍了有关JS中的0,null,undefined,[],{},'',false之间的关系,需要的朋友可以参考下

    JavaScript typeof, null, 和 undefined

    JavaScript typeof, null, 和 undefined JavaScript typeof, null, undefined, valueOf()。 typeof 操作符 你可以使用 typeof 操作符来检测变量的数据类型。 实例 typeof "John" // 返回 string typeof 3.14...

    Javascript常用小技巧汇总

    本文实例讲述了Javascript常用小技巧。分享给大家供大家参考。具体分析如下: 一、True 和 False 布尔表达式 下面的布尔表达式都返回 false: null undefined ” 空字符串 0 数字0 但小心下面的, 可都返回 true: ‘0...

    Javascript 中的false零值nullundefined和空字符串对象

    Javascript 中的false 、零值、null、undefined和空字符串对象的区别

    JavaScript Undefined,Null类型和NaN值区别

    一 Undefined 类型 只有一种值 undefined 以下三种情况typeof 返回类型为undefined 1. 当变量未初始化时 2. 变量未定义时 3. 函数无明确返回值时(函数没有返回值时返回的都是undefined) undefined 并不等同于...

    compact-object:仅返回具有“非空”值的对象

    var object = { a : 'b' , foo : null , b : false , taco : 1 , qux : 0 , c : '' , d : [ ] , yo : { wut : { a : null , c : 54 } , lolz : undefined }} ;console . log ( compact ( object ) ) ;// { a : 'b' ,...

    02 数据类型的转换.html

    数据类型:基本类型:数字number、字符串:string、布尔:boolean、null、undefined ...空的对象:数字0 字符'' null undefined 字符串的转换:String() 不管什么类型都相当于在之前的基础上加了一个引号“”

    JS中怎样判断undefined(比较不错的方法)

    所以我就自然的用这个值和字符串”undefined”去比较,很不幸,比较的结果是false. 没办法,只能google一下,寻求大神了. 原来必须用typeof,写法就是: 代码如下: if(typeof(value)==”undefined”){ alert&#40;...

Global site tag (gtag.js) - Google Analytics