javascript tutorial - [Solved-5 Solutions] Check if a value is an object in javascript - javascript - java script - javascript array
Problem:
How to check if a value is an Object in JavaScript?
Solution 1:
Try using typeof(var) and/or var instanceof something.
Solution 2:
If typeof yourVariable === 'object', it's an object or null. If we want to exclude null, just make it yourVariable !== null && typeof yourVariable === 'object'.
Solution 3:
Object.prototype.toString.call(myVar) will return:
- "[object Object]" if myVar is an object
- "[object Array]" if myVar is an array
- etc.
Solution 4:
The official underscore.js uses this check to find out if something is really an object
Solution 5:
Little late... for "plain objects" (we mean, like {'x': 5, 'y': 7}) we have this little snippet:
It generates the next output:
It always works for me. If will return "true" only if the type of "o" is "object", but no null, or array, or function.