Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
1  
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 '09 at 9:20
    
@Andy It seems that my answer is not the best. Maybe you should select a different answer as accepted? –  Peter Smit Aug 7 '11 at 18:10
    
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. –  Andy McCluggage Aug 8 '11 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]" –  Andy McCluggage Aug 8 '11 at 14:43
    
I think that in a world where performance it's usually what's most important it's worth seeing some results of checking if a variable is array. Obviously .toString() should be avoided as much as possible, so if you're not dealing with multiple frames you should use instanceof. –  rolandjitsu Aug 17 '13 at 15:19
add comment

19 Answers

up vote 537 down vote accepted

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.

share|improve this answer
11  
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 '09 at 9:21
13  
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 '12 at 22:33
    
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 '13 at 15:12
    
To reinforce @BYK's point, this only works with objects in the same frame. Don't rely on this if you are working with multiple frames! –  Cory Klein Sep 25 '13 at 15:08
    
Doesn't work in some cases: Array.prototype instanceof Array; // false (should be true); also ({ __proto__ : Array.prototype }) instanceof Array; // true (should be false) –  vorrtex Nov 4 '13 at 18:15
show 1 more comment

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 toString.call(obj) === "[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.

share|improve this answer
    
See this article for a good discussion on the topic. The conclusion is to use this solution. –  Nick G. Mar 12 at 17:53
add comment

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

function isArray(o) {
  return Object.prototype.toString.call(o) === '[object Array]'; 
}
share|improve this answer
8  
Come on, that method is so, well, unappropriate. –  BYK Apr 20 '09 at 9:16
    
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 '09 at 9:20
3  
Therefor the link is given so that Brett can check them out and see in which case his function has to work –  Peter Smit Apr 20 '09 at 9:23
1  
@icCube Please, tell why it is not the way to go! –  Peter Smit Aug 8 '11 at 3:08
1  
See my answer below. I recommend Peter Smit's way. –  Brian Jun 29 '12 at 19:28
show 2 more comments

Via Crockford:

function typeOf(value) {
    var s = typeof value;
    if (s === 'object') {
        if (value) {
            if (value instanceof Array) {
                s = 'array';
            }
        } else {
            s = 'null';
        }
    }
    return s;
}

The main failing Crockford mentions is an inability to correctly determine arrays that were created in a different context, e.g., window. That page has a much more sophisticated version if this is insufficient.

share|improve this answer
add comment

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

share|improve this answer
4  
@Vitimtk A prototype acts as a fallback for the actual object, so this should work even if the array in question already existed. It won't work before the source line is processed, of course. –  Markus Bruckner Dec 28 '11 at 19:39
23  
Assuming no one does Object.prototype.isArray = true;! :( –  ErikE Sep 30 '12 at 3:08
8  
@ErikE that made me lol. this.trollcode = true –  destiel starship Nov 18 '12 at 13:44
2  
Genius. +1 to your awesomeness :) –  Jashwant Mar 13 '13 at 7:19
2  
Note that in ES5 Array.isArray is a method (e.g., Array.isArray([1,2,3]) === true) so @ErikE wasn't being a troll. I would avoid following this answer as it will break code in some modern browsers. –  JaredMcAteer Jul 11 '13 at 14:00
show 5 more comments

I personally like Peter's suggestion: http://stackoverflow.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 = Object.prototype.toString.call([]);
    if(check_class === '[object Array]')
    {
        // test passed, now check
        return Object.prototype.toString.call(o) === '[object Array]';
    }
    else
    {
        // 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 Object.prototype.toString.call() 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.

share|improve this answer
    
Good call on the ECMAScript 5 standard. Sure you can't guarantee the browser supports it but this should be the first way to check in new code. –  styfle Dec 13 '12 at 22:43
add comment

When I posted this question the version of JQuery that I was using didn't include an isArray function. If it had have I would have probably just used it trusting that implementation to be the best browser independant way to perform this particular type check.

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

$.isArray(obj);

(as of version 1.6.2) It is still implemented using comparisons on strings in the form

toString.call(obj) === "[object Array]"
share|improve this answer
add comment

I was using this line of code:

if (variable.push) {
   // variable is array, since AMAIK only arrays have push() method.
}
share|improve this answer
    
This is not a good solution at all. With this "solution" any object with a property push that is truthy will be considered an Array. –  teleclimber Apr 28 at 17:41
add comment

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

e.g.,

Array.isArray([])    // true
Array.isArray("foo") // false
Array.isArray({})    // false
share|improve this answer
add comment

In modern browsers you can do

Array.isArray(obj)

(Supported by Chrome 5, Firefox 4.0, IE 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.toString.call(obj) === '[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)
share|improve this answer
    
This is probably the best most modern approach. I've seen it along with the polyfill on MDN so that means Mozilla trusts it developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… –  John Apr 12 at 0:13
add comment

code referred from https://github.com/miksago/Evan.js/blob/master/src/evan.js

  var isArray = Array.isArray || function(obj) {
    return !!(obj && obj.concat && obj.unshift && !obj.callee);};
share|improve this answer
    
Why do you test both concat and unshift, wouldn't suffice to test for unshift? –  Marco Demaio Feb 18 at 14:55
    
The more methods we check that Array has the move likely it really is an array. Other objects might have concat or unshift but less likely to have both. –  Kris Apr 24 at 13:35
add comment

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 http://documentcloud.github.com/underscore/#isArray).

_.isArray(object) 

Returns true if object is an Array.

share|improve this answer
    
The underscore js implementation uses the native Array.isArray if available, otherwise it uses the toString method. –  Kris Apr 24 at 13:28
add comment

In Crockford's JavaScript The Good Parts, there is a function to check if the given argument is an array:

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

He explains:

First, we ask if the value is truthy. We do this to reject null and other falsy values. Second, we ask if the typeof value is 'object'. This will be true for objects, arrays, and (weirdly) null. Third, we ask if the value has a length property that is a number. This will always be true for arrays, but usually not for objects. Fourth, we ask if the value contains a splice method. This again will be true for all arrays. Finally, we ask if the length property is enumerable (will length be produced by a for in loop?). That will be false for all arrays. This is the most reliable test for arrayness that I have found. It is unfortunate that it is so complicated.

share|improve this answer
add comment

For those who code-golf, an unreliable test with fewest characters:

function isArray(a) {
  return a.map;
}

This is commonly used when traversing/flattening a hierarchy:

function golf(a) {
  return a.map?[].concat.apply([],a.map(golf)):a;
}

input: [1,2,[3,4,[5],6],[7,[8,[9]]]]
output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
share|improve this answer
add comment

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.

share|improve this answer
1  
Would you mind explaining how that works? Mainly, what does the '+' do? –  Andy McCluggage Mar 3 at 14:21
    
This is good, but it is also true when the object is a function or a string as well as any other object with property length of type number. Why? Well, the unary + operator actually casts a variable as a number. So basically they are checking if obj.length is a number. [object Object] has no property length it is undefined so when you cast undefined as a number it becomes NaN the above check comes out as false. So it returns true if the object property length is a number, which in the case of arrays, strings, and functions it would be true. Underscore has to be doing more than just this. –  John Apr 12 at 0:36
add comment

I liked the Brian answer:

function is_array(o){
    // make sure an array has a class attribute of [object Array]
    var check_class = Object.prototype.toString.call([]);
    if(check_class === '[object Array]')    {
        // test passed, now check
        return Object.prototype.toString.call(o) === '[object Array]';
    } else{
        // may want to change return value to something more desirable
        return -1; 
    }
}

but you could just do like this:

return Object.prototype.toString.call(o) === Object.prototype.toString.call([]);
share|improve this answer
add comment
function isArray(x){
    return ((x != null) && (typeof x.push != "undefined"));
}
share|improve this answer
1  
Why the votedown? Tell me, what is wrong with this, so I can learn too. –  Mészáros Lajos Oct 3 '13 at 10:32
1  
push is not really an uncommon name. If I add such a method to prototypes or define own classes with that method, they will be arrays according to your implementation. Short example: The very not-array-ish object { push: 'Trolled' }. –  Ingo Bürk Nov 30 '13 at 17:18
    
Ah, okay, thank you for your explanation. Never thought of this before. –  Mészáros Lajos Dec 3 '13 at 9:46
add comment

If you are using Angular, you can use the angular.isArray() function

var myArray = [];
angular.isArray(myArray); // returns true

var myObj = {};
angular.isArray(myObj); //returns false

http://docs.angularjs.org/api/ng/function/angular.isArray

share|improve this answer
add comment

A better solution, would be to test for methods you will need to be using. Using the in keyword. That way, if the you later define an array-like object you can still use the same function on it. For example, the only method this function needs to be implemented to treat the argument as an array is map:

function print(object) {
  if ('map' in object) {         // If object implements map treat it as an array
    object.map(function(current) { 
      console.log(current);
    });
  } else {                       // Otherwise, treat it as a string
    console.log(object);
  }
}

This principle is called duck typing, as in "When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck." It is a widely used principle in many high level languages, notably Python. From their documentation: Python has a strong tradition of duck-typing, where explicit type-checking is never done and code simply calls methods on an object, trusting that those methods will be there and raising an exception if they aren’t. I haven't been able to do any specific tests, but simply checking whether an object has a specific property is surely faster than checking the type at runtime.

share|improve this answer
add comment

protected by Alma Do Mar 26 at 10:47

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.