JavaScript中 null undefined 小结

一、 前言

因为对javascript中的 null\undefined\” 有些分不清楚,因此在试验之后进行一下小结。

二、 源码

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width">
        <title></title>
        <script type="text/javascript" charset="utf-8">
            var a; 
            var b = null; 
            var c = {}; 
            var d = ''; 
            console.log(x); //报错 
            console.log(a); //undefined 
            console.log(b); //null 
            console.log(c.somevalue); //undefined 
            console.log(d); // 
            console.log(!a, !b, !d); //true true true 
        </script>
      </head>
      <body>
      </body>
    </html>

三、 总结

  1. 未定义变量: 使用会报错
  2. 声明变量未赋值: 变量为undefined
  3. 声明变量并赋值为null: 变量为null
  4. **对象中未声明的变量:**变量为undefined
  5. **空字符串:**变量为”
  6. null\undefined\” 的非: 均为true

四、 参考链接

what is the difference between null and undefined in JavaScript? - Stack Overflow

本文章迁移自http://blog.csdn.net/timberwolf_2012/article/details/45701245

/** * RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS. * LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/ /* var disqus_config = function () { this.page.url = PAGE_URL; // Replace PAGE_URL with your page's canonical URL variable this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable }; */ (function() { // DON'T EDIT BELOW THIS LINE var d = document, s = d.createElement('script'); s.src = 'https://chenzz.disqus.com/embed.js'; s.setAttribute('data-timestamp', +new Date()); (d.head || d.body).appendChild(s); })();