How to check if a variable is an array in JavaScript?

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:


How to check if a variable is an array in JavaScript?

To check if a variable is an array or not in JavaScript, you could also use:

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

This seems to be 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).

Answer #3:

There are multiple solutions with all their own quirks. One possible solution is:

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

Answer #4:

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)

Answer #5:

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:

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.

Answer #6:

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.

Answer #7:

If you’re only dealing with EcmaScript 5 and above then you can use the built in Array.isArray function


Array.isArray([])    // true
Array.isArray("foo") // false
Array.isArray({})    // false

Answer #8:

Let’s have a look at this solution mentioned above:

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

However, 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.

How to check if a variable is an array using JQuery?

Since JQuery now does offer this function, I would always use it…


(as of version 1.6.2) It is still implemented using comparisons on strings in the form === "[object Array]"

Answer #9:

The universal solution is below:'[object Array]'

Starting from ECMAScript 5, a formal solution is :


Also, for old JavaScript libs, you can find below solution although it’s not accurate enough:

var is_array = function (value) {
    return value &&
    typeof value === 'object' &&
    typeof value.length === 'number' &&
    typeof value.splice === 'function' &&

Hope you learned something from this post.

Follow Programming Articles for more!

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →