8

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

function isArray(o) { ??? }

Damien
  • 115
  • 5
  • 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? – Landon Kuhn May 28 '10 at 21:39

6 Answers6

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]';
  };
}
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • even safer is `return Object.prototype.toString.call(obj) === '[object Array]';` to avoid any possible coersion – Rixius May 29 '10 at 02:35
  • @Rixius: Well, the `Object.prototype.toString` method is [fully described](http://bclary.com/2004/11/07/#a-15.2.4.2) 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... – Christian C. Salvadó May 29 '10 at 02:53
  • Someone could have bashed the `Object.prototype.toString` always better to be safe than sorry. – Rixius May 29 '10 at 18:26
  • 3
    @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. – Christian C. Salvadó May 29 '10 at 19:00
1

You should be able to use the instanceof operator:

var testArray = [];

if (testArray instanceof Array)
    ...
Chad Birch
  • 73,098
  • 23
  • 151
  • 149
  • 1
    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](http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/) for more details. – Christian C. Salvadó May 28 '10 at 21:56
1

jQuery solves lots of these sorts of issues:

jQuery.isArray(obj)

aceofspades
  • 7,568
  • 1
  • 35
  • 48
0

This is what I use:

function is_array(obj) {
  return (obj.constructor.toString().indexOf("Array") != -1)
}
Alex Korban
  • 14,916
  • 5
  • 44
  • 55
  • Thanks for answer, I dont understand why x.constructor.toString().indexOf("Array") returns 9 if its array instance? can you please tell me? – Sudarshan Kalebere Feb 27 '17 at 19:12
0
function typeOf(obj) {
  if ( typeof(obj) == 'object' )
    if (obj.length)
      return 'array';
    else
      return 'object';
    } else
  return typeof(obj);
}
Justin
  • 4,434
  • 4
  • 28
  • 37
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;
}
Serty Oan
  • 1,737
  • 12
  • 19
  • 1
    Prototype is not using that method anymore, see [here](http://github.com/sstephenson/prototype/blob/1.6.1/src/lang/object.js#L191) how it's implemented in 1.6.1. – Christian C. Salvadó May 28 '10 at 22:30