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

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

    Some precisions:

    null and undefined are two different values. One is representing the absence of a value for a name and the other is representing the absence of a name.


    What happens in an if goes as follows for if( o ):

    The expression in the parentheses o is evaluated, and then the if kicks in type-coercing the value of the expression in the parentheses - in our case o.

    Falsy (that will get coerced to false) values in JavaScript are: '', null, undefined, 0, and false.

提交回复
热议问题