`
sony-soft
  • 浏览: 1105673 次
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

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.csdn.net/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
分享到:
评论

相关推荐

    my97日历控件

    specialDates:null,specialDays:null,disabledDates:null,disabledDays:null,onpicking:null,onpicked:null,onclearing:null,oncleared:null,ychanging:null,ychanged:null,Mchanging:null,Mchanged:null,dchanging:...

    JavaScript中的undefined学习总结

    这一点对于理解JavaScript中变量和对象属性的行为至关重要,因此需要对此进行深入学习。 首先,我们需要明白何时会遇到undefined值。在JavaScript中,当你尝试访问一个尚未被声明的变量时,你会得到undefined。例如...

    javascript知识点总结《一》

    ### JavaScript知识点总结《一》 #### 第一章:初步认识JavaScript ##### 学习目标: - **理解JavaScript的特点** - **学会三种JavaScript的引入方式** ##### JavaScript的特点: 1. **了解特点前:** JavaScript...

    JavaScript 80道面试题和答案.docx

    在JavaScript编程语言中,...总之,理解和掌握JavaScript中的`undefined`和`null`、逻辑运算符以及DOM的基本概念对于JavaScript开发者来说至关重要,这些知识点不仅在面试中经常出现,也是编写高效、健壮代码的基础。

    javascript基本数据结构

    4. 空值(null):表示没有任何值,不同于其他语言中的`undefined`。 JavaScript还允许使用常量,例如: - 整型常量:10、0x1A(十六进制)、077(八进制)。 - 实型常量:3.14159、1.2e3(1200)。 - 布尔常量:`...

    javascript简写常用的12个技巧(可以大大减少你的js代码量).docx

    在JavaScript编程中,经常会遇到需要验证一个变量是否为`null`或`undefined`的情况。传统的做法可能会显得较为冗长: ```javascript if (variable1 !== null && variable1 !== undefined && variable1 !== '') { ...

    JavaScript学习深入—面向对象编程

    - `false`:`false`、`0`、`""`(空字符串)、`null`、`undefined`、`NaN`。 - `true`:除了上述被视为`false`的值外的所有值。 #### 三、面向对象编程在JavaScript中的应用 面向对象编程(OOP)在JavaScript中的...

    JavaScript基础练习_day1

    在JavaScript中,`null` 和 `undefined` 使用宽松相等运算符`==`比较时会被认为是相等的。 - `null == false` 的返回值为 `false`。`null` 和 `false` 不相等。 #### 五、JavaScript的基本数据类型 JavaScript中...

    Sortable前端框架

    Sortable is a &lt;s&gt;minimalist&lt;/s&gt; JavaScript library for reorderable drag-and-drop lists. Demo: http://rubaxa.github.io/Sortable/ ## Features * Supports touch devices and [modern]...

    JavaScript语言教程.docx

    ### JavaScript语言教程知识点详解 #### 一、JavaScript简介 JavaScript是一种功能强大且广泛应用的编程语言,以其轻量级和灵活性著称。它最初是为了增强Web页面的交互性而设计的,但现在已被广泛应用于多种环境,...

    js,数组,对象,构造函数,json字符串

    1:2`中,如果`n`为真(非null,非undefined,非false,非0,非NaN),则返回1;否则返回2。在这种情况下,由于`n`被设置为`null`,因此条件为假,打印结果为2。 理解并熟练运用这些基本概念,对于进行JavaScript...

    javascript学习笔记

    ### JavaScript 学习笔记知识点概览 #### 一、JavaScript 的基本概念与运行方式 - **JavaScript** 是一种脚本语言,主要用于网页的交互性设计,由 Netscape 公司开发。 - **Java Applet** 是由 Sun Microsystems ...

    javascript基础教程

    - **null**和**undefined**:分别表示没有值和未定义的值。 3. **类型转换**:JavaScript会在必要时自动进行类型转换,但有时也需要显式地进行类型转换,比如使用`parseInt()`和`parseFloat()`将字符串转换为数值...

    javascript 教程-学习笔记.docx

    ### JavaScript 教程知识点概述 #### 一、JavaScript 基础语法介绍 **1.1 双标签引入外部 JS 文件** 在 HTML 中,我们可以通过 `&lt;script&gt;` 标签来引入外部的 JavaScript 文件。例如: ```html &lt;script src="my....

    大名鼎鼎SWFUpload- Flash+JS 上传

    当Flash上传文件的时候,由开发人员预定义的Javascript事件会被定时触发以便来更新页面中的UI,同时还提供上传状态和错误信息。 选定的文件的上传和它所在页面、表单是独立的。每个文件都是单独上传的,这就保证了...

    unity3d脚本中文基础 javascapt语法基础

    在JavaScript中,有多种基本数据类型,包括字符串(String)、数字(Number)、布尔值(Boolean)、null、undefined以及特殊的对象类型。变量的声明使用var关键字,例如: ```javascript var myString = "Hello, ...

    你有必要知道的 25 个 JavaScript 面试题

    **模块化开发**:在 jQuery 或 Node.js 的插件开发中,IIFE 常用于创建私有作用域,防止变量污染。 - **示例**:使用 IIFE 解决定时器中的变量污染问题。 ```javascript for (var i = 0; i ; i++) { (function...

    前端大厂最新面试题-data_type (1).docx

    在 JavaScript 中 null 表示 "什么都没有",是一个只有一个值的特殊类型,表示一个空对象引用,而 undefined 表示一个没有设置值的变量。默认情况下 null 和 undefined 是所有类型的子类型, 就是说你可以把 null 和...

    js使用小技巧

    取变量类型 typeof($js_libpath) == "undefined" 下拉框 下拉框.options[索引] 下拉框.options.length 查找对象 document.getElementsByName("r1"); document.getElementById(id); 定时 timer=setInterval...

Global site tag (gtag.js) - Google Analytics