Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
We’ve seen throughout this chapter that arrays are objects
with some special behavior. Given an unknown object, it is often useful to
be able to determine whether it is an array or not. In ECMAScript 5, you
can do this with the Array.isArray() function:
Array.isArray([]) // => true
Array.isArray({}) // => false
We can write an isArray()
function that works in any version of JavaScript like this:
var isArray = Array.isArray || function(o) {
var ts = Object.prototype.toString;
return typeof o === "object" &&
ts.call(o) === "[object Array]";
};