The Wayback Machine - https://web.archive.org/web/20240121110711/https://www.geeksforgeeks.org/how-to-check-object-is-an-array-in-javascript/
Open In App
Related Articles

How to check object is an array in JavaScript ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In this article, we will see how to check whether the given object is an array or not in JavaScript. There are two approaches, these are:

  • Using Array.isArray() Method
  • Using typeof Operator

Approach 1: Using Array.isArray() Method

The Array.isArray() method determines whether the value passed to this function is an array or not. This method returns true if the argument passed is an array else it returns false.

 Syntax:

Array.isArray( obj )

Parameter:

  •  obj is any valid object in JavaScript like map, list, array, string, etc. 

Return Value: It returns Boolean value true if the object passed is an array or false if the object passed is not an array. 

Example 1: This example uses Array.isArray() method to check the object is array or not. 

Javascript




function checkObject() {
    const countries = ["India", "USA", "Canada"];
    const checkArrayObj = Array.isArray(countries);
 
    console.log(checkArrayObj);
}
 
checkObject();


Output

true

Example 2: This example uses Array.isArray() function to check the object is array or not. 

Javascript




function checkObject() {
         
    // It returns false as the object
    // passed is String not an array
    console.log( Array.isArray(
        'hello GeeksForGeeks' )
    );
}
 
checkObject();


Output

false

Approach 2: Using typeof Operator

In JavaScript, the typeof operator returns the data type of its operand in the form of a string where an operand can be any object, function or variable. However, the problem with this is that it isn’t applicable for determining array. 

Syntax:

typeof operand or typeof( operand )

Example: 

Javascript




console.log(
    typeof "Geeks" + "\n" +
    typeof [1, 2, 3, 4] + "\n" +
    typeof {name:'Kartik', age:20} + "\n" +
    typeof new Date() + "\n" +
    typeof function () {} + "\n" +
    typeof job + "\n" +
    typeof null
);


Output

string
object
object
object
function
undefined
object


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 25 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials