Why is null an object and what's the difference between null and undefined?

后端 未结 22 1519
猫巷女王i
猫巷女王i 2020-11-22 02:58

Why is null considered an object in JavaScript?

Is checking

if ( object == null )
      Do something

the

22条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 03:31

    Comparison of many different null checks in JavaScript:

    http://jsfiddle.net/aaronhoffman/DdRHB/5/

    // Variables to test
    var myNull = null;
    var myObject = {};
    var myStringEmpty = "";
    var myStringWhiteSpace = " ";
    var myStringHello = "hello";
    var myIntZero = 0;
    var myIntOne = 1;
    var myBoolTrue = true;
    var myBoolFalse = false;
    var myUndefined;
    
    ...trim...
    

    http://aaron-hoffman.blogspot.com/2013/04/javascript-null-checking-undefined-and.html

    JavaScript Null Check Comparison Chart

提交回复
热议问题