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
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