How to check if an object is not an array?

hojberg picture hojberg · Feb 15, 2010 · Viewed 47.1k times · Source

So i have a function that needs to check if an argument is an object, but this fails because:

typeof [] // returns 'object'

This is a classic javascript gotcha, but i cant remember what to do to actually accept objects, but not arrays.

Answer

Tomas Vana picture Tomas Vana · Feb 15, 2010

Try something like this :

obj.constructor.toString().indexOf("Array") != -1

or (even better)

obj instanceof Array