Array and object are the only inputs . Is there a simple function that can determine whether a variable is an array or object?
            Asked
            
        
        
            Active
            
        
            Viewed 167 times
        
    1
            
            
        - 
                    1have you checked this? - http://stackoverflow.com/questions/8834126/how-to-efficiently-check-if-variable-is-array-or-object-in-nodejs-v8 – Freddie Fabregas Apr 16 '13 at 04:18
- 
                    http://blog.niftysnippets.org/2010/09/say-what.html .. worth looking – alwaysLearn Apr 16 '13 at 04:18
4 Answers
3
            I suspect there are many other similar answers but this is one way:
if ({}.toString.call(obj) == '[object Object]') {
  // is an object
}
if ({}.toString.call(obj) == '[object Array]') {
  // is an array
}
This can be turned into a nifty function:
function typeOf(obj) {
  return {}.toString.call(obj).match(/\w+/g)[1].toLowerCase();
}
if (typeOf(obj) == 'array') ...
This works for any type:
if (typeOf(obj) == 'date') // is a date
if (typeOf(obj) == 'number') // is a number
...
 
    
    
        elclanrs
        
- 92,861
- 21
- 134
- 171
- 
                    +1, but you should use `Object.prototype.toString` as the `toString` method of the global/window object may not produce an appropriate result. :-) – RobG Apr 16 '13 at 04:28
- 
                    I guess it depends on how much browser support you need. This should work on modern browsers AFAIK. It defaults to `window.toString` which is an object. Otherwise I'd prefer `{}.toString`. But you're right. – elclanrs Apr 16 '13 at 04:29
- 
                    In Firefox, `window.toString.call(obj)` returns `[xpconnect wrapped native prototype]`. Note that `window` is a host object so not bound to comply with rules for native objects. Also, the [global object](http://www.ecma-international.org/ecma-262/5.1/#sec-15.1) is not an instance of Object (it doesn't have a `[[Prototype]]` property defined by ECMA-262 either), it's a special object that exists before any other. – RobG Apr 16 '13 at 04:31
- 
                    Just updated to use the proper (shorter) way that should work on all modern browsers. AFAIK only IE7- has problems that can be easily solved with `({}).toString`. I've been using `window.toString` without problems tho in browsers that I need to target, but I see the trouble. – elclanrs Apr 16 '13 at 04:34
- 
                    I guess the last caveat is that this may not work with host objects, it may even throw an error, but it should be OK in the OP's case. – RobG Apr 16 '13 at 05:32
1
            
            
        (variable instanceof Array) will return true for arrays.
You could also use variable.isArray(), but this is not supported by older browsers.
 
    
    
        Christophe
        
- 27,383
- 28
- 97
- 140
1
            
            
        You can use Array.isArray():
if(Array.isArray(myVar)) {
    // myVar is an array
} else {
    // myVar is not an array
}
As long as you know it will be one or the other you are set.  Otherwise, combine this with typeof:
if(typeof myVar === "object") {
    if(Array.isArray(myVar)) {
        // myVar is an array
    } else {
        // myVar is a non-array object
    }
}
 
    
    
        Jeff B
        
- 29,943
- 7
- 61
- 90
1
            
            
        First check if it is an instanceof Array and then if it of object type.
if(variable instanceof Array)
{
//this is an array. This needs to the first line to be checked 
//as an array instanceof Object is also true
}
else if(variable instanceof Object)
{
//it is an object
}
 
    
    
        PSL
        
- 123,204
- 21
- 253
- 243
- 
                    Fails if the object is passed across a frame as it will be created from a different instance of Array and Object. – RobG Apr 16 '13 at 04:26
