How do you check if an object is an array or not?

The best way to find out whether or not an object is an instance of a particular class is to use the toString method from Object.prototype:

  var arrayList = [1,2,3];

One of the best use cases of type-checking an object is when we do method overloading in JavaScript. For example, let’s say we have a method called greet, which takes one single string and also a list of strings. To make our greet method workable in both situations, we need to know what kind of parameter is being passed. Is it a single value or a list of values?

function greet(param){
 	if(){ // here have to check whether param is array or not 
 	}else{
 	}
}

However, as the implementation above might not necessarily check the type for arrays, we can check for a single value string and put some array logic code in the else block. For example:

function greet(param){
 	if(typeof param === 'string'){ 
 	}else{
 	  // If param is of type array then this block of code would execute
 	}
}

Now it’s fine we can go with either of the aforementioned two implementations, but when we have a situation where the parameter can be single valuearray, and object type, we will be in trouble.

Coming back to checking the type of an object, as mentioned previously we can use
Object.prototype.toString

if( Object.prototype.toString.call( arrayList ) === '[object Array]' ) {
    console.log('Array!');
}

If you are using jQuery, then you can also use the jQuery isArray method:

if($.isArray(arrayList)){
    console.log('Array');
}else{
  	console.log('Not an array');
}

FYI, jQuery uses Object.prototype.toString.call internally to check whether an object is an array or not.

In modern browsers, you can also use

Array.isArray(arrayList);

Array.isArray is supported by Chrome 5, Firefox 4.0, IE 9, Opera 10.5 and Safari 5

Asked on January 18, 2019 in Javascript.
Add Comment


  • 7 Answer(s)

    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
    Answered on January 19, 2019.
    Add Comment

    JavaScript Array: Exercise-1 with Solution
    Write a JavaScript function to check whether an ‘object’ is an array or not.

    Test Data:
    console.log(is_array(‘w3resource’));
    console.log(is_array([1, 2, 4, 0]));
    false
    true

    Sample Solution:

    HTML Code:

    JS Bin

    Copy
    JavaScript Code:

    var is_array = function(input) {
    if (toString.call(input) === “[object Array]”)
    return true;
    return false;
    };
    console.log(is_array(‘w3resource’));
    console.log(is_array([1, 2, 4, 0]));

    Copy
    Sample Output:

    false
    true

    Answered on January 27, 2019.
    Add Comment

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

    function whatAmI(me){ return Object.prototype.toString.call(me).split(/\W/)[2]; }

    // tests
    console.log(
    whatAmI([“aiming”,”@”]),
    whatAmI({living:4,breathing:4}),
    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
    }

    Answered on January 27, 2019.
    Add Comment

    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?

    Answered on January 28, 2019.
    Add Comment

    Todd Motto showed me an interesting way to deal with this: the  Object.prototype.toString() method.

    This method converts the object type into a string. And in JavaScript, confusingly, everything is an object—not just proper objects ({}).

    To make this work, you use call() to call the toString() method on your array.

    var arr = ['tuna', 'chicken', 'pb&j'];
    var obj = {sandwich: 'tuna', chips: 'cape cod'};
    
    // Returns '[object Array]'
    Object.prototype.toString.call(arr);
    
    // Returns '[object Object]'
    Object.prototype.toString.call(obj);
    Answered on February 3, 2019.
    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.prototype.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

    Answered on February 4, 2019.
    Add Comment

    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.prototype.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
    Answered on February 5, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.