How to check if an object is an array?

I'm trying to write a function that either accepts a list of strings, or a single string. If it's a string, then I want to convert it to an array with just the one item. Then I can loop over it without fear of an error.

So how do I check if the variable is an array?

I've rounded up the various solutions below and created a jsperf test.



In modern browsers you can do


(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 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

The method given in the ECMAScript standard to find the class of Object is to use the toString method from Object.prototype.

if( someVar ) === '[object Array]' ) {
    alert( 'Array!' );

Or you could use typeof to test if it is a String:

if( typeof someVar === 'string' ) {
    someVar = [ someVar ];

Or if you're not concerned about performance, you could just do a concat to a new empty Array.

someVar = [].concat( someVar );

There's also the constructor which you can query directly:

if ( == "Array") {
    // do something

Check out a thorough treatment from @T.J. Crowder's blog, as posted in his comment below.

Check out this benchmark to get an idea which method performs better:

From @Bharath convert string to array using Es6 for the question asked:

const convertStringToArray = (object) => {
   return (typeof object === 'string') ? Array(object) : object 


let m = 'bla'
let n = ['bla','Meow']
let y = convertStringToArray(m)
let z = convertStringToArray(n)
console.log('check y: '+JSON.stringify(y)) . // check y: ['bla']
console.log('check y: '+JSON.stringify(z)) . // check y: ['bla','Meow']

I would first check if your implementation supports isArray:

if (Array.isArray)
    return Array.isArray(v);

You could also try using the instanceof operator

v instanceof Array

jQuery also offers an $.isArray() method:

var a = ["A", "AA", "AAA"];

if($.isArray(a)) {
  alert("a is an array!");
} else {
  alert("a is not an array!");
<script src=""></script>


This is the fastest among all methods (all browsers supported):

function isArray(obj){
    return !!obj && obj.constructor === Array;

Imagine you have this array below:

var arr = [1,2,3,4,5];

Javascript (new and older browsers):

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


function isArray(arr) {
  return arr instanceof Array;


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

then call it like this:


Javascript (IE9+, Ch5+, FF4+, Saf5+, Opera10.5+)






Underscore and Lodash:


Array.isArray works fast, but it isn't supported by all versions of browsers. So you could make an exception for others and use universal method:

    Utils = {};    
    Utils.isArray = ('isArray' in Array) ? 
        Array.isArray : 
        function (value) {
            return === '[object Array]';

Simple function to check this:

function isArray(object)
    return object.constructor === Array;

As MDN says in here:

use Array.isArray or to differentiate regular objects from arrays

Like this:

  • === '[object Array]', or

  • Array.isArray(arr)


There's just one line solution for this question

x instanceof Array

where x is the variable it will return true if x is an array and false if it is not.


You can check the type of your variable whether it is an array with;

var myArray=[];

if(myArray instanceof Array)

I would make a function to test the type of object you are dealing with...

function whatAmI(me){ return\W/)[2]; }

// tests
  whatAmI(function(ing){ return ing+" to the global window" }),
  whatAmI("going to do with you?")

// output: Array Object Function String

then you can write a simple if statement...

if(whatAmI(myVar) === "Array"){
    // do array stuff
} else { // could also check `if(whatAmI(myVar) === "String")` here to be sure
    // do string stuff

I do this in a very simple way. Works for me. Any drawbacks?

Array.prototype.isArray = true;

a=[]; b={};
a.isArray  // true
b.isArray  // (undefined -> false)

This is my attempt to improve on this answer taking into account the comments:

var isArray = myArray && myArray.constructor === Array;

It gets rid of the if/else, and accounts for the possibility of the array being null or undefined


Array.isArray = Array.isArray || function (vArg) {
    return === "[object Array]";

I have updated the jsperf fiddle with two alternative methods as well as error checking.

It turns out that the method defining a constant value in the 'Object' and 'Array' prototypes is faster than any of the other methods. It is a somewhat surprising result.

/* Initialisation */
Object.prototype.isArray = function() {
  return false;
Array.prototype.isArray = function() {
  return true;
Object.prototype._isArray = false;
Array.prototype._isArray = true;

var arr = ["1", "2"];
var noarr = "1";

/* Method 1 (function) */
if (arr.isArray()) document.write("arr is an array according to function<br/>");
if (!noarr.isArray()) document.write("noarr is not an array according to function<br/>");
/* Method 2 (value) - **** FASTEST ***** */
if (arr._isArray) document.write("arr is an array according to member value<br/>");
if (!noarr._isArray) document.write("noarr is not an array according to member value<br/>");

These two methods do not work if the variable takes the undefined value, but they do work if you are certain that they have a value. With regards to checking with performance in mind if a value is an array or a single value, the second method looks like a valid fast method. It is slightly faster than 'instanceof' on Chrome, twice as fast as the second best method in Internet Explorer, Opera and Safari (on my machine).


I know, that people are looking for some kind of raw javascript approach. But if you want think less about, take a look here:


Returns true if object is an Array.

(function(){ return _.isArray(arguments); })();
=> false
=> true

The best solution I've seen is a cross-browser replacement for typeof. Check Angus Croll's solution here.

The TL;DR version is below, but the article is a great discussion of the issue so you should read it if you have time.

Object.toType = function(obj) {
    return ({})\s([a-z|A-Z]+)/)[1].toLowerCase();
// ... and usage:
Object.toType([1,2,3]); //"array" (all browsers)

// or to test...
var shouldBeAnArray = [1,2,3];
if(Object.toType(shouldBeAnArray) === 'array'){/* do stuff */};

Here's my lazy approach:

if (Array.prototype.array_ === undefined) {
  Array.prototype.array_ = true;

// ...

var test = [],
    wat = {};

console.log(test.array_ === true); // true
console.log(wat.array_ === true);  // false

I know it's sacrilege to "mess with" the prototype, but it appears to perform significantly better than the recommended toString method.

Note: A pitfall of this approach is that it wont work across iframe boundaries, but for my use case this is not an issue.


There is a nice example in Stoyan Stefanov's book JavaScript Patterns which suppose to handle all possible problems as well as utilize ECMAScript 5 method Array.isArray().

So here it is:

if (typeof Array.isArray === "undefined") {
    Array.isArray = function (arg) {
        return === "[object Array]";

By the way, if you are using jQuery, you can use it's method $.isArray()


easiest and fastest way to check if an Object is an Array or not.

 var arr = []; ==='Array'  //return true;


arr.constructor ===Array //return true;

or you can make a utility function:

function isArray(obj){ return obj && obj.constructor ===Array}


isArray(arr); //return true

The following could be used if you know that your object doesn't have a concat method.

var arr = [];
if (typeof arr.concat === 'function') {
    console.log("It's an array");


You could is isArray method but I would prefer to check with

Object.getPrototypeOf(yourvariable) === Array.prototype


If the only two kinds of values that could be passed to this function are a string or an array of strings, keep it simple and use a typeof check for the string possibility:

function someFunc(arg) {
    var arr = (typeof arg == "string") ? [arg] : arg;
A = [1,2,3]

In search for shortest version here is what I got so far.

Note, there is no perfect function that will always detect all possible combinations. It is better to know all abilities and limitations of your tools than expect a magic tool.

function isArray(value) {
    if (value) {
        if (typeof value === 'object') {
            return ( == '[object Array]')
    return false;

var ar = ["ff","tt"]

A simple function for testing if an input value is an array is the following:

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

This works cross browser, and with older browsers. This is pulled from T.J. Crowders' blog post


You can try this:

var arr = []; (or) arr = new Array();
var obj = {}; (or) arr = new Object();

arr.constructor.prototype.hasOwnProperty('push') //true

obj.constructor.prototype.hasOwnProperty('push') // false

This function will turn almost anything into an array:

function arr(x) {
    if(x === null || x === undefined) {
        return [];
    if(Array.isArray(x)) {
        return x;
    if(isString(x) || isNumber(x)) {
        return [x];
    if(x[Symbol.iterator] !== undefined || x.length !== undefined) {
        return Array.from(x);
    return [x];

function isString(x) {
    return === "[object String]"

function isNumber(x) {
    return === "[object Number]"

It uses some newer browser features so you may want to polyfill this for maximum support.


> arr(null);
> arr(undefined)
> arr(3.14)
[ 3.14 ]
> arr(1/0)
[ Infinity ]
> gen = function*() { yield 1; yield 2; yield 3; }
[Function: gen]
> arr(gen())
[ 1, 2, 3 ]
> arr([4,5,6])
[ 4, 5, 6 ]
> arr("foo")
[ 'foo' ]

N.B. strings will be converted into an array with a single element instead of an array of chars. Delete the isString check if you would prefer it the other way around.

I've used Array.isArray here because it's the most robust and also simplest.


In your case you may use concat method of Array which can accept single objects as well as array (and even combined):

function myFunc(stringOrArray)
  var arr = [].concat(stringOrArray);


  arr.forEach(function(item, i)
    console.log(i, "=", item);

myFunc("one string");

myFunc(["one string", "second", "third"]);

concat seems to be one of the oldest methods of Array (even IE 5.5 knows it well).


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.