0

I am using the following code to check if an element is an object in javascript:
element.constructor == Object Is this a correct way or there are more appropriate ways
to check that? An alternate would be to use this - typeof element === 'object'
There seem to be multiple ways to do that not sure which one is more appropriate

sunny
  • 149
  • 8
  • 3
    Does this answer your question? [Check if a value is an object in JavaScript](https://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript) – ahsan Sep 10 '21 at 11:27
  • Thanks, I will check that. Separately I found that most javascript documentation is available on https://developer.mozilla.org/ Is there any documentation for chrome too? – sunny Sep 10 '21 at 11:38

1 Answers1

0

It would be the easiest if you use the lodash package. Lodash is quite common in the js community and I really can recommend this in most cases.

A modern JavaScript utility library delivering modularity, performance & extras.

Lodash docs of isObject function: https://lodash.com/docs/4.17.15#isObject

Checks if value is the language type of Object. (e.g. arrays, functions, objects, regexes, new Number(0), and new String(''))

Example

_.isObject({});
// => true
 
_.isObject([1, 2, 3]);
// => true
 
_.isObject(_.noop);
// => true
 
_.isObject(null);
// => false
Ling Vu
  • 4,740
  • 5
  • 24
  • 45