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

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

    Use null to define something as having no value, use undefined when you expect something might not be defined at all.

    For example, if a variable has no value, assign it as null.

    var weDontHaveAValue = null;
    

    If you expect that something might be not defined at all, e.g. an optional options argument, use undefined.

    if (typeof args.optionalParam !== 'undefined') { }
    

提交回复
热议问题