What's the 'right' way to tell if an object is an Array?
function isArray(o) { ??? }
What's the 'right' way to tell if an object is an Array?
function isArray(o) { ??? }
The best way:
function isArray(obj) {
  return Object.prototype.toString.call(obj) == '[object Array]';
}
The ECMAScript 5th Edition Specification defines a method for that, and some browsers, like Firefox 3.7alpha, Chrome 5 Beta, and latest WebKit Nightly builds already provide a native implementation, so you might want to implement it if not available:
if (typeof Array.isArray != 'function') {
  Array.isArray = function (obj) {
    return Object.prototype.toString.call(obj) == '[object Array]';
  };
}
return Object.prototype.toString.call(obj) === '[object Array]'; to avoid any possible coersionObject.prototype.toString method is fully described in the specification, a String return value is guaranteed, I don't see any benefit of using the strict equals operator, when you know you are comparing two strings values...Object.prototype.toString always better to be safe than sorry.Object.prototype.toString = function () {return "[object Array]"; }; even with the strict equals === operator the function will return true always. Crockford says: "always use ===", I say: learn about type coercion to decide which operator use.You should be able to use the instanceof operator:
var testArray = [];
if (testArray instanceof Array)
    ...
instanceof is when you work in a multi-frame DOM environment, an array object form one frame is not instance of the Array constructor of other frame. See this article for more details.This is what I use:
function is_array(obj) {
  return (obj.constructor.toString().indexOf("Array") != -1)
}
You can take take Prototype library definition of method Object.isArray() which test it :
function(object) {
  return object != null && typeof object == "object" &&
   'splice' in object && 'join' in object;
}