Categories
arrays javascript javascript-objects

How can I check if an object is an array? [duplicate]

3237

I’m trying to write a function that either accepts a list of strings, or a single string. If it’s a string, then I want to convert it to an array with just the one item so I can loop over it without fear of an error.

So how do I check if the variable is an array?

12

  • 8

    I thought you meant to ‘check if object is an array’, but you want to check if ‘object is an array of strings or a single string’ specifically. Not sure if you see it? Or is it just me? I was thinking of something more like this… am I the one missing something here?

    – rr1g0

    Jul 23, 2015 at 18:23

  • 178

    TL;DRarr.constructor === Array is fastest.

    – Neta

    Nov 23, 2015 at 22:37

  • 4

    jsben.ch/#/QgYAV – a benchmark for the most common ways

    Oct 24, 2016 at 17:34

  • 53

    TL;DR – Array.isArray(arr) since ES5; and $.isArray(arr) in jQuery.

    Dec 19, 2016 at 9:55

  • 7

    Just bear in mind that if you by any reason overwrite your constructor via prototype that arr.constructor === Array test will return false. Array.isArray(arr) still returns true though.

    – ghaschel

    Feb 23, 2018 at 17:44

1501

In modern browsers you can do:

Array.isArray(obj)

(Supported by Chrome 5, Firefox 4.0, Internet Explorer 9, Opera 10.5 and Safari 5)

For backward compatibility you can add the following:

// Only implement if no native implementation is available
if (typeof Array.isArray === 'undefined') {
  Array.isArray = function(obj) {
    return Object.prototype.toString.call(obj) === '[object Array]';
  }
};

If you use jQuery you can use jQuery.isArray(obj) or $.isArray(obj). If you use Underscore.js you can use _.isArray(obj).

If you don’t need to detect arrays created in different frames you can also just use instanceof:

obj instanceof Array

3

  • 11

    Here is a more complete list of browsers that support Array.isArray

    Dec 17, 2014 at 18:51

  • 28

    @NobleUplift: instanceof Array fails if the array is from a different frame because every array from that different frame has a different Array constructor and prototype. For compatibility/security reasons, every frame has its own global environment, and this includes global objects. The Object global from one frame is different from the Object global from another. So too for Array globals. Axel Rauschmayer talks more about this.

    – jschoi

    Sep 22, 2018 at 7:45

  • 1

    Note that, as of 2022, this is very well supported (even back many browser versions): caniuse.com/?search=isArray And also now the fastest method: jsben.ch/QgYAV

    – Andrew

    May 20 at 21:54

1501

In modern browsers you can do:

Array.isArray(obj)

(Supported by Chrome 5, Firefox 4.0, Internet Explorer 9, Opera 10.5 and Safari 5)

For backward compatibility you can add the following:

// Only implement if no native implementation is available
if (typeof Array.isArray === 'undefined') {
  Array.isArray = function(obj) {
    return Object.prototype.toString.call(obj) === '[object Array]';
  }
};

If you use jQuery you can use jQuery.isArray(obj) or $.isArray(obj). If you use Underscore.js you can use _.isArray(obj).

If you don’t need to detect arrays created in different frames you can also just use instanceof:

obj instanceof Array

3

  • 11

    Here is a more complete list of browsers that support Array.isArray

    Dec 17, 2014 at 18:51

  • 28

    @NobleUplift: instanceof Array fails if the array is from a different frame because every array from that different frame has a different Array constructor and prototype. For compatibility/security reasons, every frame has its own global environment, and this includes global objects. The Object global from one frame is different from the Object global from another. So too for Array globals. Axel Rauschmayer talks more about this.

    – jschoi

    Sep 22, 2018 at 7:45

  • 1

    Note that, as of 2022, this is very well supported (even back many browser versions): caniuse.com/?search=isArray And also now the fastest method: jsben.ch/QgYAV

    – Andrew

    May 20 at 21:54

1316

I would first check if your implementation supports isArray:

if (Array.isArray)
    return Array.isArray(v);

You could also try using the instanceof operator

v instanceof Array

2

  • 133

    v instanceof Array will return false if v was created in another frame (v is instance of thatFrame.contentWindow.Array class).

    – pepkin88

    Jan 3, 2012 at 2:08

  • 48

    To be specific: Array.isArray is defined as part of ECMAScript 5/Javascript 1.8.5.

    – jevon

    Oct 23, 2012 at 5:38