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

2
  • 3
    Does this answer your question? Check if a value is an object in JavaScript Commented Sep 10, 2021 at 11:27
  • Thanks, I will check that. Separately I found that most javascript documentation is available on developer.mozilla.org Is there any documentation for chrome too? Commented Sep 10, 2021 at 11:38

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.