What is the difference between null and undefined in JavaScript?

前端 未结 30 3321
夕颜
夕颜 2020-11-21 23:06

I want to know what the difference is between null and undefined in JavaScript.

30条回答
  •  忘了有多久
    2020-11-21 23:47

    null and undefined are two distinct object types which have the following in common:

    • both can only hold a single value, null and undefined respectively;
    • both have no properties or methods and an attempt to read any properties of either will result in a run-time error (for all other objects, you get value undefined if you try to read a non-existent property);
    • values null and undefined are considered equal to each other and to nothing else by == and != operators.

    The similarities however end here. For once, there is a fundamental difference in the way how keywords null and undefined are implemented. This is not obvious, but consider the following example:

    var undefined = "foo";
    WScript.Echo(undefined); // This will print: foo
    

    undefined, NaN and Infinity are just names of preinitialized "superglobal" variables - they are initialized at run-time and can be overridden by normal global or local variable with the same names.

    Now, let's try the same thing with null:

    var null = "foo"; // This will cause a compile-time error
    WScript.Echo(null);
    

    Oops! null, true and false are reserved keywords - compiler won't let you use them as variable or property names

    Another difference is that undefined is a primitive type, while null is an object type (indicating the absense of an object reference). Consider the following:

    WScript.Echo(typeof false); // Will print: boolean
    WScript.Echo(typeof 0); // Will print: number
    WScript.Echo(typeof ""); // Will print: string
    WScript.Echo(typeof {}); // Will print: object
    WScript.Echo(typeof undefined); // Will print: undefined
    WScript.Echo(typeof null); // (!!!) Will print: object
    

    Also, there is an important difference in the way null and undefined are treated in numeric context:

    var a; // declared but uninitialized variables hold the value undefined
    WScript.Echo(a === undefined); // Prints: -1
    
    var b = null; // the value null must be explicitly assigned 
    WScript.Echo(b === null); // Prints: -1
    
    WScript.Echo(a == b); // Prints: -1 (as expected)
    WScript.Echo(a >= b); // Prints: 0 (WTF!?)
    
    WScript.Echo(a >= a); // Prints: 0 (!!!???)
    WScript.Echo(isNaN(a)); // Prints: -1 (a evaluates to NaN!)
    WScript.Echo(1*a); // Prints: -1.#IND (in Echo output this means NaN)
    
    WScript.Echo(b >= b); // Prints: -1 (as expected)
    WScript.Echo(isNaN(b)); // Prints: 0 (b evaluates to a valid number)
    WScript.Echo(1*b); // Prints: 0 (b evaluates to 0)
    
    WScript.Echo(a >= 0 && a <= 0); // Prints: 0 (as expected)
    WScript.Echo(a == 0); // Prints: 0 (as expected)
    WScript.Echo(b >= 0 && b <= 0); // Prints: -1 (as expected)
    WScript.Echo(b == 0); // Prints: 0 (!!!)
    

    null becomes 0 when used in arithmetic expressions or numeric comparisons - similarly to false, it is basically just a special kind of "zero". undefined, on the other hand, is a true "nothing" and becomes NaN ("not a number") when you try to use it in numeric context.

    Note that null and undefined receive a special treatment from == and != operators, but you can test true numeric equality of a and b with the expression (a >= b && a <= b).

提交回复
热议问题