Detecting an undefined object property

后端 未结 30 2921
花落未央
花落未央 2020-11-21 04:43

What\'s the best way of checking if an object property in JavaScript is undefined?

30条回答
  •  太阳男子
    2020-11-21 05:39

    From lodash.js.

    var undefined;
    function isUndefined(value) {
      return value === undefined;
    }
    

    It creates a local variable named undefined which is initialized with the default value -- the real undefined, then compares value with the variable undefined.


    Update 9/9/2019

    I found Lodash updated its implementation. See my issue and the code.

    To be bullet-proof, simply use:

    function isUndefined(value) {
      return value === void 0;
    }
    

提交回复
热议问题