Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  5] [ 2]  / answers: 1 / hits: 63505  / 15 Years ago, mon, february 15, 2010, 12:00:00

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.


More From » types

 Answers
23

Try something like this :



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


or (even better)



obj instanceof Array

[#97567] Friday, February 12, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
francokeganr

Total Points: 438
Total Questions: 102
Total Answers: 124

Location: Belarus
Member since Sat, Jul 18, 2020
4 Years ago
;