下面的例子中
var message;  
alert(message);  //”undefined”
alert(age);   //causes an error
但是如果用typeof来看的话,那么:
两个都是'undefined',alert和typeof差异怎么这么大?

解决方案 »

  1.   

    有区别:运行下面代码看看区别var TestVar;
    alert(TestVar); //shows undefined
    alert(typeof TestVar); //shows undefinedvar TestVar = null;
    alert(TestVar); //shows null
    alert(typeof TestVar); //shows object
      

  2.   

    下面是一段英文对话,来说明区别的You: What is name?
    JavaScript: name? What's a name? I don't know what you're talking about. You haven't ever mentioned any name before. Are you seeing some other scripting language on the (client-)side?name = null;
    You: What is name?
    JavaScript: I don't know.
    In short; undefined is where no notion of the thing exists; it has no type, and it's never been referenced before in that scope; null is where the thing is known to exist, but it's not known what the value is.
    One thing to remember is that null is not, conceptually, the same as false or "" or such, even if they equate after type casting, i.e.name = false;
    You: What is name?
    JavaScript: Boolean false.name = '';
    You: What is name?
    JavaScript: Empty string
    http://stackoverflow.com/questions/801032/why-is-null-an-object-and-whats-the-difference-compared-to-undefined
      

  3.   

    我喜欢用语言及列举来解释他们的区别,给你参考,区别在于世界上从来没存在过一个叫A的人 -> undefined世界上有一个 A 的人,现在他死了 -> null
      

  4.   

    undefined是表示空引用,null是表示空值。
      

  5.   

    undefined表示忘记给东西了
    null表示说给你点东西,但你手里还是没东西
      

  6.   

    null 已定义,并初始化为null
    undefined:未定义,或者未初始化
      

  7.   

    var message;  // 定义变量 message 未初始化
    alert(message);  //获取变量的值 ”undefined”
    alert(age);   //获取变量的值 age 未定义,报错 causes an error
    但是如果用typeof来看的话,那么:
    两个都是'undefined',alert和typeof差异怎么这么大?typeof // 获取变量的 数据类型
      

  8.   

    这个跟null和undefined没有关系吧,主要是:alert时,获取的是变量的值,如果变量未声明的话,就会报errortypeof,获取的是变量的数据类型,任何情况,都不可能报error
      

  9.   

    null 已经注册,但是没有任何指向
    undefine 没有注册