8

What's the 'right' way to tell if an object is an Array?

function isArray(o) { ??? }

1
  • 1
    You might want to provide more detail about your environment, e.g.: pure javascript? Running in a browser? Is prototype or other libraries available? Commented May 28, 2010 at 21:39

6 Answers 6

9

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]';
  };
}
Sign up to request clarification or add additional context in comments.

4 Comments

even safer is return Object.prototype.toString.call(obj) === '[object Array]'; to avoid any possible coersion
@Rixius: Well, the Object.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...
Someone could have bashed the Object.prototype.toString always better to be safe than sorry.
@Rixius, well, if someone replaced the built-in method, there is not too much to do, imagine: 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.
1

You should be able to use the instanceof operator:

var testArray = [];

if (testArray instanceof Array)
    ...

1 Comment

The only downside of 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.
1

jQuery solves lots of these sorts of issues:

jQuery.isArray(obj)

Comments

0

This is what I use:

function is_array(obj) {
  return (obj.constructor.toString().indexOf("Array") != -1)
}

1 Comment

Thanks for answer, I dont understand why x.constructor.toString().indexOf("Array") returns 9 if its array instance? can you please tell me?
0
function typeOf(obj) {
  if ( typeof(obj) == 'object' )
    if (obj.length)
      return 'array';
    else
      return 'object';
    } else
  return typeof(obj);
}

Comments

0

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;
}

1 Comment

Prototype is not using that method anymore, see here how it's implemented in 1.6.1.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.