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
-
3Does this answer your question? Check if a value is an object in JavaScriptahsan– ahsan2021-09-10 11:27:26 +00:00Commented 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?sunny– sunny2021-09-10 11:38:01 +00:00Commented Sep 10, 2021 at 11:38
Add a comment
|
1 Answer
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