Check if a value is an object in JavaScript

后端 未结 30 3856
臣服心动
臣服心动 2020-11-22 05:06

How do you check if a value is an object in JavaScript?

30条回答
  •  梦如初夏
    2020-11-22 05:56

    const isObject = function(obj) {
      const type = typeof obj;
      return type === 'function' || type === 'object' && !!obj;
    };
    

    !!obj is shorthand for checking if obj is truthy (to filter out null)

提交回复
热议问题