懂得js中的:Null、undefined、""0、false

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

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 表示一个未声明的变量,或已声明但没有赋值的变量,或一个并不存在的对象属性。

-----------------------------------------------

书籍资料:

《JavaScript核心技术》机械工业出版社 2007年6月 第一版第一次印刷

0、""、NaN、null和defined都是假的 。剩下的东西都是真的。

换句话说,零、null、NaN和空字符串天生就是假 ;而其他的天生就是真 。

================================================

测试实例:

<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');  
           
          } 
         
    网络资源路径:
    http://topic.csdn.net/t/20031230/12/2617647.html
    *
    *
    */
   
   
   
   
   
     //=============================================================================
     //各种类型
     //_____________________________________________________________________________
    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