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

后端 未结 22 1063
猫巷女王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:40

    typeof null;      // object
    typeof undefined; // undefined
    

    The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy for boolean operations.

    var x = null;
    var y;
    

    x is declared & defined as null

    y is declared but not defined. It is declared with no value so it is undefined.

    z is not declared so would also be undefined if you attempted to use z.

提交回复
热议问题