I'm trying to check if a variable is an object like so:
if(obj && typeof obj === Object) {
console.log('obj is an object and does not return null value');
}
what am i missing?
typeof returns a string representation of the type, but if you want to check for null then
if(typeof obj === 'object' && obj !== null) {
console.log('obj is an object and does not return null value');
}
true for Arrays too. If you want a strict check that it's an Object you can use '[object Object]' == Object.prototype.toString.call(o).typeof on old versions of Safari sometimes returns 'function' on non-function objects. So you also need to test for typeof obj=='function'It should be;
typeof obj === 'object'
The typeof operator uses strings as identifiers. You can read more about it on MDN.
the best way to do it is using instanceOf, best practice
if(obj instanceof Object) {
console.log('obj is an object and does not return null value');
}