How to check if an object is an array or not in JavaScript

JavaScript program to check if an object is an Array:

In this post, we will learn different ways to check if an object is an Array or not. The typeof operator returns object for an Array as it is a non-primitive data type.

For example,

let a1 = [1, 2, 3, 4];
let a2 = [{i: 1}, 2, 3];
let a3 = 5;
let a4 = 'hello';
let a5 = undefined;
let a6 = {'name': 'Alex'};

console.log(typeof a1); // object
console.log(typeof a2); // object
console.log(typeof a3); // number
console.log(typeof a4); // string
console.log(typeof a5); // undefined
console.log(typeof a6); // object

If you run this program, it will print object for the arrays a1 and a2. The easiest way to check if an object is array is by using the Array.isArray method.

How to use Array.isArray() method to check if an object is an Array or not:

The Array.isArray() method is used to check if an object is array or not. The syntax of the Array.isArray() method is:

Array.isArray(value)

It takes one value as its parameter and returns one boolean value. It returns true if value is an Array. Else, it returns false.

Let’s try it with different parameters:

let a1 = [1, 2, 3, 4];
let a2 = [{i: 1}, 2, 3];
let a3 = 5;
let a4 = 'hello';
let a5 = undefined;
let a6 = {'name': 'Alex'};

console.log(Array.isArray(a1)); // true
console.log(Array.isArray(a2)); // true
console.log(Array.isArray(a3)); // false
console.log(Array.isArray(a4)); // false
console.log(Array.isArray(a5)); // false
console.log(Array.isArray(a6)); // false

As this is a boolean value, we can use it with any conditional statement:

let a1 = [1, 2, 3, 4];
let a2 = [{i: 1}, 2, 3];
let a3 = 5;
let a4 = 'hello';
let a5 = undefined;
let a6 = {'name': 'Alex'};

if(Array.isArray(a1)){
    console.log('a1 is an array');
}

How to use instanceOf operator to check if an object is an Array:

The instanceOf operator is used to check if an object is an instance of a class or not. It is used to check the instance runtime. It returns one boolean value, true if the object is an instance of the class, or else false.

Let’s check how it works with the above example:

let a1 = [1, 2, 3, 4]; // true
let a2 = [{i: 1}, 2, 3]; // true
let a3 = 5; // false
let a4 = 'hello'; // false
let a5 = undefined; // false
let a6 = {'name': 'Alex'}; // false

console.log(a1 instanceof Array);
console.log(a2 instanceof Array);
console.log(a3 instanceof Array);
console.log(a4 instanceof Array);
console.log(a5 instanceof Array);
console.log(a6 instanceof Array);

As you can see, it returns the same result.

Array.isArray() or instanceOf:

Although both Array.isArray() method and instanceOf operator gives the same result, Array.isArray() method is preferred to check if an object is Array over instanceOf.

The instanceOf doesn’t work across different globals like iframes. For example,

let iframe = document.createElement('iframe');
document.body.appendChild(iframe);
iframeArray = window.frames[window.frames.length - 1].Array;

let arr = new iframeArray(1, 2, 3);

console.log(arr instanceof Array);  // false    
console.log(Array.isArray(arr));  // true

If you run this, arr instanceOf Array will return false.

You might also like: