What is the difference between null and undefined in JavaScript?

前端 未结 30 4303
夕颜
夕颜 2020-11-21 23:06

I want to know what the difference is between null and undefined in JavaScript.

30条回答
  •  天命终不由人
    2020-11-21 23:27

    null and undefined are both are used to represent the absence of some value.

    var a = null;
    

    a is initialized and defined.

    typeof(a)
    //object
    

    null is an object in JavaScript

    Object.prototype.toString.call(a) // [object Object]
    
    var b;
    

    b is undefined and uninitialized

    undefined object properties are also undefined. For example "x" is not defined on object c and if you try to access c.x, it will return undefined.

    Generally we assign null to variables not undefined.

提交回复
热议问题