I have a value and want to know if it\'s an iteratable object literal, before I iterate it.
How do I do that?
Well, you don't need to check everything by yourself or write your own codes, when there are good libraries such as Lodash and Underscore.
In Lodash, you can easily check it by isPlainObject function, e.g.:
_.isPlainObject({'a': 12});
Check this page: https://lodash.com/docs#isPlainObject
Say you have some testvar
and want to see if it is an object, but not an array or null (these are both of type Object). You can do the following
testVar instanceof Object && !Array.isArray(testVar) && testVar !== null
You could also do something like:
if (someObject.constructor == Object) {
// do your thing
}
you can read more about it here
const isObject = (x) => {
return Object.prototype.toString.call(x) == "[object Object]";
}
That works for me.
If you use obj instanceof Object
or typeof obj === "object"
you get false positives on things like new Number(3)
and arrays ([1,2,3]
).
Using o.constructor === Object
is great, however there's a weird edge case of Object.create(null)
– which does, in fact, give you a plain object, albeit not one created in a "normal" way. Conceptually it gives a valid, normal, undecorated object. We check for this case with Object.getPrototypeOf(o) === null
which will only hold true for the above undecorated object type.
The !!o
converts null
or undefined
to false
. People were complaining about it above, and honestly o && ...
is more succinct and unless you're serializing it doesn't matter. Nevertheless, I included it.
function isObject(o) {
return o && o.constructor === Object
}
function isObject1(o) {
return !!o && o.constructor === Object
}
function isObject2(o) {
// edge case where you use Object.create(null) –– which gives an object {} with no prototype
return !!o && (Object.getPrototypeOf(o) === null || o.constructor === Object)
}
Bumping old thread, but it's still shows up in searches and people are even referencing it as duplicate for a new similar ones - and still the top-most answers here are far from being correct (sorry people, no offends).
To check if variable is an object the following should be used:
if (typeof variable === 'object') {
// do something
}
Arrays are also objects, so this would be true for an array too.
Moreover - null
is a valid object as well, therefore the above will return true on null
too.
Personally, when really need to check if an expected variable is 'workable' object I'm always using this boringly repeated formula:
if (variable && typeof variable === `object`) {
// do something
}
Last but not least :) please please please, do yourself a favor and don't use any libraries for such a simple things. Javascript is a fastly evolving language having today much much more than yesterday, so fast that most of the libraries are not even fast enough to fetch up. Beside it, people who are working on the spec are doing a great job and mostly the native APIs are clean, correct, making perfect sense and coherent with the rest of the language.