

Also note that if you're going to worry about toString()'s implementation changing, you should also worry about every other built in method changing too. Note that in JavaScript The Definitive Guide 6th edition, 7.10, it says Array.isArray() is implemented using () in ECMAScript 5. may want to change return value to something more desirable make sure an array has a class attribute of returns false if it passes test and it's not an array returns true if it passes test and it's an array If you really want to be specific and make sure toString() has not been changed, and there are no problems with the objects class attribute ( is the class attribute of an object that is an array), then I recommend doing something like this: //see if toString returns proper class attributes of objects that are arrays

For ECMAScript 5, use Array.isArray())Ĭomments on the post indicate, however, that if toString() is changed at all, that way of checking an array will fail. I personally like Peter's suggestion: (for ECMAScript 3. I wanted to make sure the original link stay for whenever jsperf comes back online. Note: has created another test as is down.

However, since you're looking for an array, just use the fastest method above.Īlso, I ran some test: So have some fun and check it out. However, this is a one stop shop for any type you're looking for. This guy is the slowest for trying to check for an Array. Update: instanceof now goes 2/3 the speed! Note that checking for numbers does not work as variable instanceof Number always returns false. Still pretty solid, looks cleaner, if you're all about pretty code and not so much on performance. This method runs about 1/3 the speed as the first example. you can read more about it here variable instanceof Array Solid to use! Quite impressed by the outcome. This guy is about 2-5% slower, but it's pretty hard to tell. Running about 1/5 the speed as the first example. This last one is, in my opinion the ugliest, and it is one of the slowest fastest. Update using Chrome 75, shout out to for having me revisit this with his question Some other ways are: Array.isArray(variable) If you are having issues with finding out if an objects property is an array, you must first check if the property is there. All arrays are objects, so checking the constructor property is a fast process for JavaScript engines. This is the fastest method on Chrome, and most likely all other browsers. The best solution is the one you have chosen. There are several ways of checking if an variable is an array or not.
