javascript当中null和undefined的==和===的比较

3.1.3(null和undefined的==和===的比较)



<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<script>
    //马克-to-win:声明变量x
    /* if a value is not set, its typeof is undefined, its value is undefined, if a value= null, its typeof is object, its value
is null,but when you use == to test, they are the same, but when to
use === to test, they are not the same,== means as long as value is the same, ok, but === means type also must be equal. */
    var x;
    var z1 = "d";
    var y = null;

    //      document.writeln("z1 is"+z1);
    /*if you cancel commenting out the following statement, it will give the blank page, which means it has error.*/
    //        document.writeln(zyx);
    document.writeln(x + " is x");
    document.writeln(typeof(x) + " is typeof(x)");
    document.writeln(y + " is y");
    document.writeln(typeof(y) + " is typeof(y)");
    var z = undefined;
    document.writeln(z + " is z");
    document.writeln(typeof(z) + " is typeof(z)");
    if (y == undefined)
    {
        document.writeln('null and undefined is interchangable');
    }
    if (z1 != null)
    {
        document.writeln('z1 != null');
    }
    if (y === undefined)
    {
        document.writeln('null and undefined is exactly the same');
    }
    if (x == undefined)
    {
        document.writeln('声明变量后默认值为undefined');
    }

    if (x === undefined)
    {
        document.writeln('声明变量后默认值为exactly the same as undefined');
    }

    if (x == null)
    {
        document.writeln('声明变量后默认值为null');
    }

    if (x === null)
    {
        document.writeln('马克-to-win:声明变量后默认值为exactly the same as null, note that null\' s typeof is Object.');
    }

    if (x == y)
    {
        document.writeln("x等于y");
    }
    if (x === z)
    {
        document.writeln("x三等于z");
    }
</script>


输出结果:
undefined is x undefined is typeof(x) null is y object is typeof(y) undefined is z undefined is typeof(z) null and undefined is interchangable z1 != null 声明变量后默认值为undefined 声明变量后默认值为exactly the same as undefined 声明变量后默认值为null x等于y x三等于z