arrays object string - How do you check if a variable is an array in JavaScript?

11 Answers

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.


As of ES5 there is now also:


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

to or empty

This question already has an answer here:

I would like to check whether a variable is either an array or a single value in JavaScript.

I have found a possible solution...

if (variable.constructor == Array)...

Is this the best way this can be done?

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

function isArray(o) {
  return === '[object Array]'; 

This is an old question but having the same problem i found a very elegant solution that i want to share.

Adding a prototype to Array makes it very simple

Array.prototype.isArray = true;

Now once if you have an object you want to test to see if its an array all you need is to check for the new property

var box = doSomething();

if (box.isArray) {
    // do something

isArray is only available if its an array

I personally like Peter's suggestion: https://.com/a/767499/414784 (for ECMAScript 3. For ECMAScript 5, use Array.isArray())

Comments on the post indicate, however, that if toString() is changed at all, that way of checking an array will fail. If you really want to be specific and make sure toString() has not been changed, and there are no problems with the objects class attribute ([object Array] is the class attribute of an object that is an array), then I recommend doing something like this:

//see if toString returns proper class attributes of objects that are arrays
//returns -1 if it fails test
//returns true if it passes test and it's an array
//returns false if it passes test and it's not an array
function is_array(o)
    // make sure an array has a class attribute of [object Array]
    var check_class =[]);
    if(check_class === '[object Array]')
        // test passed, now check
        return === '[object Array]';
        // may want to change return value to something more desirable
        return -1; 

Note that in JavaScript The Definitive Guide 6th edition, 7.10, it says Array.isArray() is implemented using in ECMAScript 5. Also note that if you're going to worry about toString()'s implementation changing, you should also worry about every other built in method changing too. Why use push()? Someone can change it! Such an approach is silly. The above check is an offered solution to those worried about toString() changing, but I believe the check is unnecessary.

Thought I would add another option for those who might already be using the Underscore.js library in their script. Underscore.js has an isArray() function (see


Returns true if object is an Array.

I was using this line of code:

if (variable.push) {
   // variable is array, since AMAIK only arrays have push() method.

From w3schools:

function isArray(myArray) {
    return myArray.constructor.toString().indexOf("Array") > -1;

I have created this little bit of code, which can return true types.

I am not sure about performance yet, but it's an attempt to properly identify the typeof. also blogged a little about it here

it works, similar to the current typeof.

var user = [1,2,3]
typeOf(user); //[object Array]

It think it may need a bit of fine tuning, and take into account things, I have not come across or test it properly. so further improvements are welcomed, whether it's performance wise, or incorrectly re-porting of typeOf.

Since the .length property is special for arrays in javascript you can simply say

obj.length === +obj.length // true if obj is an array

Underscorejs and several other libraries use this short and simple trick.