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

JS真假值详解

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MY 总结:理解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和空字符串天生就是假 ;而其他的天生就是真 。

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

测试实例:

  1. <html>
  2. <head>
  3. <TITLE>解决Null和undefined等问题</TITLE>
  4. <scripttype= "text/javascript" >
  5. var str= "Howareyoudoingtoday?"
  6. document.write(str.split( "" )+ "<br/>" )
  7. document.write(str.split( "" )+ "<br/>" )
  8. document.write(str.split( "" ,3))
  9. /**
  10. *这里有一题目:JS中,如何判断一个对象的值是不是NULL?
  11. *
  12. 解:
  13. if(!obj||obj=='null'||typeof(object)=="undefined"))
  14. {
  15. alert('NULL');
  16. }
  17. 网络资源路径:
  18. http://topic.youkuaiyun.com/t/20031230/12/2617647.html
  19. *
  20. *
  21. */
  22. //=============================================================================
  23. //各种类型
  24. //_____________________________________________________________________________
  25. document.write( "<br>" );
  26. document.write( "各种类型:" );
  27. document.write( "<br>" );
  28. if ( null ==undefined){document.write( "<br><br>null==undefined为ture<br>" )}
  29. if ( typeof (undefined)== 'undefined' )document.write( "typeof(undefined)=='undefined'为true<br>" );
  30. if ( typeof ( null )== 'object' ){document.write( "typeof(null)=='object'为ture<br>" )}
  31. if ( typeof ( "" )== 'string' )document.write( "typeof(/"/")=='string'为true<br>" )
  32. if ( typeof (0)== 'number' ){document.write( "typeof(0)=='number'为true<br>" )}
  33. if ( typeof ( false )== 'boolean' ){document.write( "typeof(false)=='boolean'为true<br><br>" )}
  34. /*
  35. 以上段运行结果:
  36. null==undefined为ture
  37. typeof(undefined)=='undefined'为true
  38. typeof(null)=='object'为ture
  39. typeof("")=='string'为true
  40. typeof(0)=='number'为true
  41. typeof(false)=='boolean'为true
  42. */
  43. //===============================================
  44. //测试何时if(判断条件为false)
  45. //______________________________________________
  46. document.write( "<br>" );
  47. document.write( "测试何时if(判断条件为false)" );
  48. document.write( "<br>" );
  49. if ( null ){document.write( "if(null)<br>" )}
  50. if (undefined){document.write( "if(undefined)<br>" )}
  51. if ( "" ){document.write( 'if("")<br>' )}
  52. if ( "123" ){document.write( 'if("123")<br>' )}
  53. if (0){document.write( 'if(0)<br>' )}
  54. if (1){document.write( "if(1)<br>" )}
  55. if ( true ){document.write( "if(true)<br>" )}
  56. if ( false ){document.write( 'f(false)<br>' )}
  57. //if(){}
  58. /*
  59. 以上段运行结果:
  60. if("123")
  61. if(1)
  62. if(true)
  63. 结论:
  64. ★★★★★★undefined、null、""、0、false这五个值在if语句中做判断,都会执行false分支
  65. */
  66. //=======================================================
  67. //undefined和null与“算数”运算符
  68. //_______________________________________________________
  69. document.write( "<br>" );
  70. document.write( "undefined和null与“算数”运算符" );
  71. document.write( "<br>" );
  72. document.write(10+ null + "<br>" );
  73. document.write(10+undefined);
  74. document.write( "<br>" );
  75. /*
  76. 以上段运行结果:
  77. 10
  78. NaN
  79. undefined和null比较特殊,
  80. 虽然null的类型是object,但是null不具有任何对象的特性,
  81. 就是说我们并不能执行null.toString()、null.constructor等对象实例的默认调用。
  82. 所以从这个意义上来说,null和undefined有最大的相似性。
  83. ★★看看null==undefined的结果(true)也就更加能说明这点。
  84. 不过相似归相似,还是有区别的,
  85. 就是和数字运算时,10+null结果为:10;10+undefined结果为:NaN。
  86. */
  87. //=====================================================================================
  88. //""、0和false虽然在if语句表现为"假值",可它们都是有意义数据,只是被作为了"空值"或"假值",
  89. //___________________________________________________________________________________
  90. document.write( '""、0和false虽然在if语句表现为"假值",可它们都是有意义数据,只是被作为了"空值"或"假值"' );document.write( "<br>" );
  91. document.write( '"".toString():' + "" .toString());document.write( "<br>" );
  92. document.write( "(0).toString():" +(0).toString());document.write( "<br>" );
  93. document.write( "false.toString():" + false .toString());document.write( "<br>" );
  94. /*
  95. 以上段运行结果:
  96. 0
  97. false
  98. 结论:
  99. ""、0和false虽然在if语句表现为"假值",可它们都是有意义数据,
  100. 只是被作为了"空值"或"假值",
  101. 因为:★★"".toString(),(0).toString()和false.toString()都是合法的可执行表达式。
  102. */
  103. //=======================================================
  104. //undefined、null、""、0、false这五个值转换为String时的差异
  105. //_______________________________________________________
  106. document.write( "<br>" );
  107. document.write( 'undefined、null、""、0、false这五个值转换为String时的差异' );document.write( "<br>" );
  108. document.write( "String(undefined):" +String(undefined));document.write( "<br>" );
  109. document.write( "String(null):" +String( null ));document.write( "<br>" );
  110. document.write( 'String(""):' +String( "" ));document.write( "<br>" );
  111. document.write( "String(0):" +String(0));document.write( "<br>" );
  112. document.write( "String(false):" +String( false ));document.write( "<br>" );
  113. //==========================================
  114. //测试,当某成员并未定义,而直接使用时:此时是否为undefined,以及相关执行结果。
  115. //==========================================
  116. /**
  117. 参考:http://www.w3school.com.cn/js/jsref_undefined.asp
  118. 定义和用法
  119. undefined属性用于存放JavaScript的undefined值。
  120. 语法
  121. undefined说明
  122. 无法使用for/in循环来枚举undefined属性,也不能用delete运算符来删除它。
  123. undefined不是常量,可以把它设置为其他值。
  124. ★★★★当尝试读取不存在的对象属性时也会返回undefined。
  125. 提示和注释
  126. ★★★★提示:只能用===运算来测试某个值是否是未定义的,因为==运算符认为undefined值等价于null。
  127. ★★★★注释:null表示无值,而undefined表示一个未声明的变量,或已声明但没有赋值的变量,或一个并不存在的对象属性。
  128. */
  129. document.write( "<br>" );
  130. document.write( '测试,当某成员并未定义,而直接使用时:此时是否为undefined,以及相关执行结果。' );
  131. document.write( "<br>" );
  132. //这里的"abcd"并没有事先定义
  133. /*
  134. if(abcd){document.write("if(abcd)");}
  135. if(!abcd){document.write("if(!abcd)");}
  136. if(abcd==undefined){document.write("abcd==undefined");}
  137. if(abcd=='undefined'){document.write("abcd=='undefined'");}
  138. 注:以上4行,均没有相应信息显示!!!!!!!!!!!!!!
  139. 只有下边这行显示:typeof(abcd)=='undefined'为true
  140. if(typeof(abcd)=='undefined'){document.write("typeof(abcd)=='undefined'为true<br>");}
  141. 当然如果用alert(abcd);的话仍然没有反应,不会alert出类似"undefined"的信息
  142. */
  143. if ( typeof (abcd)== 'undefined' ){document.write( "typeof(abcd)=='undefined'为true<br>" );}
  144. var aa;
  145. document.write( "aa:" +aa+ "<br>" );
  146. if (aa==undefined){document.write( "aa==undefined为true<br>" );}
  147. if ( typeof (aa)== 'undefined' ){document.write( "typeof(aa)=='undefined'为true<br>" );}
  148. if (aa== null ){document.write( "aa==null为true<br>" );}
  149. if (aa){document.write( "if(aa)<br>" );}
  150. if (!aa){document.write( "if(!aa)<br><br>" );}
  151. var t1= "" ;
  152. var t2;
  153. if (t1===undefined){document.write( "t1isundefined<br>" );}
  154. if (t2===undefined){document.write( "t2isundefined<br>" );}
  155. </script>
  156. <LINKREL=STYLESHEETTYPE= "text/css" HREF= "resource/contract_htqd.css" >
  157. </head>
  158. <body>
  159. </body>
  160. </html>

运行结果:

  1. How,are,you,doing,today?
  2. H,o,w,,a,r,e,,y,o,u,,d,o,i,n,g,,t,o,d,a,y,?
  3. How,are,you
  4. 各种类型:
  5. null ==undefined为ture
  6. typeof (undefined)== 'undefined'true
  7. typeof ( null )== 'object' 为ture
  8. typeof ( "" )== 'string'true
  9. typeof (0)== 'number'true
  10. typeof ( false )== 'boolean'true
  11. 测试何时 if (判断条件为 false
  12. if ( "123" )
  13. if (1)
  14. if ( true )
  15. undefined和 null 与“算数”运算符
  16. 10
  17. NaN
  18. "" 、0和 false 虽然在 if 语句表现为 "假值" ,可它们都是有意义数据,只是被作为了 "空值""假值"
  19. "" .toString():
  20. (0).toString():0
  21. false .toString(): false
  22. undefined、 null"" 、0、 false 这五个值转换为String时的差异
  23. String(undefined):undefined
  24. String( null ): null
  25. String( "" ):
  26. String(0):0
  27. String( false ): false
  28. 测试,当某成员并未定义,而直接使用时:此时是否为undefined,以及相关执行结果。
  29. typeof (abcd)== 'undefined'true
  30. aa:undefined
  31. aa==undefined为 true
  32. typeof (aa)== 'undefined'true
  33. aa== nulltrue
  34. if (!aa)
  35. t2 is undefined
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值