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

ID : 475

viewed : 182

Tags : javascriptarraysvariablesjavascript

Top 5 Answer for How do you check if a variable is an array in JavaScript?

vote vote


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:


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

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 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: So have some fun and check it out.

Note: @EscapeNetscape has created another test as is down. I wanted to make sure the original link stay for whenever jsperf comes back online.

vote vote


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).

vote vote


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]';  } 
vote vote


I noticed someone mentioned jQuery, but I didn't know there was an isArray() function. It turns out it was added in version 1.3.

jQuery implements it as Peter suggests:

isArray: function( obj ) {     return === "[object Array]"; }, 

Having put a lot of faith in jQuery already (especially their techniques for cross-browser compatibility) I will either upgrade to version 1.3 and use their function (providing that upgrading doesn’t cause too many problems) or use this suggested method directly in my code.

Many thanks for the suggestions.

vote vote


In modern browsers (and some legacy browsers), you can do


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

If you need to support older versions of IE, you can use es5-shim to polyfill Array.isArray; or add the following

# only implement if no native implementation is available if (typeof Array.isArray === 'undefined') {   Array.isArray = function(obj) {     return === '[object Array]';   } }; 

If you use jQuery you can use jQuery.isArray(obj) or $.isArray(obj). If you use underscore 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 

Note: the arguments keyword that can be used to access the argument of a function isn't an Array, even though it (usually) behaves like one:

var func = function() {    console.log(arguments)        // [1, 2, 3]    console.log(arguments.length) // 3    console.log(Array.isArray(arguments)) // false !!!    console.log(arguments.slice)  // undefined (Array.prototype methods not available)    console.log([3,4,5].slice)    // function slice() { [native code] }   }  func(1, 2, 3)

Top 3 video Explaining How do you check if a variable is an array in JavaScript?