Categories
arrays javascript list variables

How do I check if a variable is an array in JavaScript?

1987

How do I check if a variable is an array in JavaScript?

if (variable.constructor == Array)

8

  • 3

    Checking for an object to be an array has some specific caveats… Peter’s answer is the only one you should use.

    – aleemb

    Apr 20, 2009 at 9:20

  • 1

    @Andy It seems that my answer is not the best. Maybe you should select a different answer as accepted?

    Aug 7, 2011 at 18:10

  • 2

    Good point Peter. I hadn’t realised your answer was receiving comments like this. I think I have long since begun to use the JQuery.isArray function when checking for arrays, and interestingly that is implemented differently to any other answer given here. I have marked the popular answer as correct.

    Aug 8, 2011 at 14:27

  • 1

    Sorry that’s wrong. I looked a little deeper and (as of version 1.6.2) JQuery still type checks using comparisons in the form…. toString.call(obj) === “[object Array]”

    Aug 8, 2011 at 14:43


  • 8

    “This question has been asked before” … NO, that question got asked AFTER this one

    – Dexygen

    Nov 12, 2017 at 0:47

1882

There are several ways of checking if an variable is an array or not. The best solution is the one you have chosen.

variable.constructor === Array

This is the fastest method on Chrome, and most likely all other browsers. All arrays are objects, so checking the constructor property is a fast process for JavaScript engines.

If you are having issues with finding out if an objects property is an array, you must first check if the property is there.

variable.prop && variable.prop.constructor === Array

Some other ways are:

Array.isArray(variable)

Update May 23, 2019 using Chrome 75, shout out to @AnduAndrici for having me revisit this with his question
This last one is, in my opinion the ugliest, and it is one of the slowest fastest. Running about 1/5 the speed as the first example. This guy is about 2-5% slower, but it’s pretty hard to tell. Solid to use! Quite impressed by the outcome. Array.prototype, is actually an array. you can read more about it here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray

variable instanceof Array

This method runs about 1/3 the speed as the first example. Still pretty solid, looks cleaner, if you’re all about pretty code and not so much on performance. Note that checking for numbers does not work as variable instanceof Number always returns false. Update: instanceof now goes 2/3 the speed!

So yet another update

Object.prototype.toString.call(variable) === '[object Array]';

This guy is the slowest for trying to check for an Array. However, this is a one stop shop for any type you’re looking for. However, since you’re looking for an array, just use the fastest method above.

Also, I ran some test: http://jsperf.com/instanceof-array-vs-array-isarray/35 So have some fun and check it out.

Note: @EscapeNetscape has created another test as jsperf.com is down. http://jsben.ch/#/QgYAV I wanted to make sure the original link stay for whenever jsperf comes back online.

30

  • 11

    Note that if you’re not sure if the variable is defined or if it could be null, be sure to do those checks first since those are the common values/objects that do not have a constructor.

    Jan 12, 2015 at 2:50

  • 7

    NOTE: ‘variable.constructor === Array’ will throw EXCEPTION if variable is null but ‘variable instanceof Array’ not!

    – GorvGoyl

    Oct 5, 2016 at 9:29

  • 5

    As of Chrome 59 isArray() seems to be significantly faster, so much so I see no reason to not use isArray() in all situations.

    Oct 10, 2017 at 9:06

  • 4

    @jemiloii I disagree about it being clear. Your updates say that instanceof runs 2/3 the speed of the original answer. Does that mean faster? Slower? There’s some ambiguity to the wording, although admittedly the context of the paragraph seems to indicate slower. I ran some benchmarks of my own by modifying the code in jsben.ch/QgYAV but those results suggested instanceof was fastest. On a related note, jsben.ch/QgYAV now links to an empty benchmark.

    – Bash

    Jul 31, 2019 at 14:37


  • 6

    so many edits make your answer unreadable. That’s why I’m downvoting…

    Jan 27, 2020 at 20:03

1122

You could also use:

if (value instanceof Array) {
  alert('value is Array!');
} else {
  alert('Not an array');
}

This seems to me a pretty elegant solution, but to each his own.

Edit:

As of ES5 there is now also:

Array.isArray(value);

But this will break on older browsers, unless you are using polyfills (basically… IE8 or similar).

13

  • 27

    I suggest, actually insist on sticking with this “instanceof” operator if you are not working with multiple frames. This is the right way of checking the object type.

    – BYK

    Apr 20, 2009 at 9:21

  • 43

    The one case where this would fail is if you were trying to test for an Array or Object since Array instanceof Object == true.

    – Pierre

    Nov 28, 2012 at 22:33

  • 1

    If you are using jQuery to pass elements with find(‘code’) or something similar you would want to check the variable with variable instanceof Object since it is not an instance of an Array.

    – tuck

    May 1, 2013 at 15:12


  • 4

    @BrettBender If you’re still active, might you update your answer to reflect that as of ES5 we have Array.isArray?

    May 22, 2014 at 3:19

  • 5

    @AndrewK see Fela Winkelmolen’s answer, which has the Array.isArray method. As for this answer, it’s probably not a great idea to morph an answer into a different answer via editing.

    – muffin

    Aug 24, 2014 at 20:45

79

There are multiple solutions with all their own quirks. This page gives a good overview. One possible solution is:

function isArray(o) {
  return Object.prototype.toString.call(o) === '[object Array]'; 
}

4

  • If you read carefully, it says this method is needed when you are working with mult-frame documents which, is not recommended. This method can easly borke on a little change in the “toString” function.

    – BYK

    Apr 20, 2009 at 9:20

  • 4

    Therefor the link is given so that Brett can check them out and see in which case his function has to work

    Apr 20, 2009 at 9:23

  • 1

    See my answer below. I recommend Peter Smit’s way.

    – Brian

    Jun 29, 2012 at 19:28

  • 6

    This method is recommended by Mozilla.

    – Hank

    May 28, 2015 at 15:52