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

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

Why is null considered an object in JavaScript?

Is checking

if ( object == null )
      Do something

the

22条回答
  •  猫巷女王i
    2020-11-22 03:37

    What is the difference between null and undefined??

    A property when it has no definition, is undefined. null is an object. Its type is object. null is a special value meaning "no value. undefined is not an object, it's type is undefined.

    You can declare a variable, set it to null, and the behavior is identical except that you'll see "null" printed out versus "undefined". You can even compare a variable that is undefined to null or vice versa, and the condition will be true:

     undefined == null
     null == undefined
    

    Refer to JavaScript Difference between null and undefined for more detail.

    and with your new edit yes

    if (object == null)  does mean the same  if(!object)
    

    when testing if object is false, they both only meet the condition when testing if false, but not when true

    Check here: Javascript gotcha

提交回复
热议问题