Guest User

Untitled

a guest
Jan 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="description" content="null vs undefined">
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width">
  7. <title>JS Bin</title>
  8. </head>
  9. <body>
  10.  
  11. <script id="jsbin-javascript">
  12. var button = document.getElementsByTagName('button');
  13. var message = 'This was previously defined';
  14. function buttonArea(container){
  15. return function(text, func){
  16. var button = document.createElement('button');
  17. button.textContent = text;
  18. button.onclick = func;
  19. container.appendChild(button);
  20. };
  21. }
  22. var addButton = buttonArea(document.getElementsByTagName('body')[0]);
  23.  
  24. addButton('default', function(){
  25. logTextWithDefault();
  26. logTextWithOr();
  27. });
  28. addButton('defined', function(){
  29. logTextWithDefault(message);
  30. logTextWithOr(message);
  31. });
  32. addButton('undefined', function(){
  33. logTextWithDefault(undefined);
  34. logTextWithOr(undefined);
  35. });
  36. addButton('null', function(){
  37. logTextWithDefault(null);
  38. logTextWithOr(null);
  39. });
  40.  
  41. addButton('log equality checks', function(){
  42. console.log(
  43. 'null === undefined', null === undefined
  44. );
  45. console.log(
  46. 'null == undefined', null == undefined
  47. );
  48. console.log(
  49. 'null === null', null === null
  50. );
  51. console.log(
  52. 'null == null', null == null
  53. );
  54. console.log(
  55. 'undefined === undefined', undefined === undefined
  56. );
  57. console.log(
  58. 'undefined == undefined', undefined == undefined
  59. );
  60. });
  61.  
  62. addButton('log type checks', function(){
  63. console.log(
  64. `typeof null: ${typeof null}`
  65. );
  66. console.log(
  67. `typeof undefined: ${typeof undefined}`
  68. );
  69. });
  70.  
  71. addButton('log coercion checks', function(){
  72. console.log(
  73. `string coercion \n null: ${null} \n undefined: ${undefined}`
  74. );
  75. console.log('number coercion of null');
  76. console.log(null + 1);
  77. console.log('number coercion of undefined');
  78. console.log(undefined + 1);
  79. console.log('boolean coercion of null');
  80. console.log(!!null);
  81. console.log('boolean coercion of undefined');
  82. console.log(!!undefined);
  83. });
  84.  
  85. addButton('declaring undefined and null', function(){
  86. //var null = 'And now for...';
  87. var undefined = 'Something completely different';
  88. var someVar;
  89. console.log('declaring null results in a error');
  90. console.log('declaring a variable or function with the name undefined...', undefined);
  91. console.log("a variable that wasn't defined is still...", someVar);
  92. someVar = undefined;
  93. console.log('but if explicitly set will get the local instance variable undefined instead of the type...', someVar);
  94. });
  95.  
  96.  
  97. var logTextWithDefault = (message='default argument') => {
  98. console.log(message);
  99. };
  100. var logTextWithOr = function(someValue){
  101. var internalValue = someValue || 'default value';
  102. console.log(internalValue);
  103. }
  104. </script>
  105.  
  106.  
  107.  
  108. <script id="jsbin-source-javascript" type="text/javascript">var button = document.getElementsByTagName('button');
  109. var message = 'This was previously defined';
  110. function buttonArea(container){
  111. return function(text, func){
  112. var button = document.createElement('button');
  113. button.textContent = text;
  114. button.onclick = func;
  115. container.appendChild(button);
  116. };
  117. }
  118. var addButton = buttonArea(document.getElementsByTagName('body')[0]);
  119.  
  120. addButton('default', function(){
  121. logTextWithDefault();
  122. logTextWithOr();
  123. });
  124. addButton('defined', function(){
  125. logTextWithDefault(message);
  126. logTextWithOr(message);
  127. });
  128. addButton('undefined', function(){
  129. logTextWithDefault(undefined);
  130. logTextWithOr(undefined);
  131. });
  132. addButton('null', function(){
  133. logTextWithDefault(null);
  134. logTextWithOr(null);
  135. });
  136.  
  137. addButton('log equality checks', function(){
  138. console.log(
  139. 'null === undefined', null === undefined
  140. );
  141. console.log(
  142. 'null == undefined', null == undefined
  143. );
  144. console.log(
  145. 'null === null', null === null
  146. );
  147. console.log(
  148. 'null == null', null == null
  149. );
  150. console.log(
  151. 'undefined === undefined', undefined === undefined
  152. );
  153. console.log(
  154. 'undefined == undefined', undefined == undefined
  155. );
  156. });
  157.  
  158. addButton('log type checks', function(){
  159. console.log(
  160. `typeof null: ${typeof null}`
  161. );
  162. console.log(
  163. `typeof undefined: ${typeof undefined}`
  164. );
  165. });
  166.  
  167. addButton('log coercion checks', function(){
  168. console.log(
  169. `string coercion \n null: ${null} \n undefined: ${undefined}`
  170. );
  171. console.log('number coercion of null');
  172. console.log(null + 1);
  173. console.log('number coercion of undefined');
  174. console.log(undefined + 1);
  175. console.log('boolean coercion of null');
  176. console.log(!!null);
  177. console.log('boolean coercion of undefined');
  178. console.log(!!undefined);
  179. });
  180.  
  181. addButton('declaring undefined and null', function(){
  182. //var null = 'And now for...';
  183. var undefined = 'Something completely different';
  184. var someVar;
  185. console.log('declaring null results in a error');
  186. console.log('declaring a variable or function with the name undefined...', undefined);
  187. console.log("a variable that wasn't defined is still...", someVar);
  188. someVar = undefined;
  189. console.log('but if explicitly set will get the local instance variable undefined instead of the type...', someVar);
  190. });
  191.  
  192.  
  193. var logTextWithDefault = (message='default argument') => {
  194. console.log(message);
  195. };
  196. var logTextWithOr = function(someValue){
  197. var internalValue = someValue || 'default value';
  198. console.log(internalValue);
  199. }</script></body>
  200. </html>
Add Comment
Please, Sign In to add comment