Search code examples
javascripttypestypeof

How to check if an object is not an array?


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.


Solution

  • Try something like this :

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

    or (even better)

    obj instanceof Array