How to Check if a Variable is an Array using JavaScript?

JavaScript is a dynamically typed language. This means we don’t have to specify the type of the variable on its declaration.

The JavaScript engine determines the type of the variable at the run time based on the type of value it stores. For eg. if you assign a numeric value, the type of the variable becomes number, if you assign a string, the type becomes string and so on.

To check the type of the variable, we use the typeof operator. It returns a string representing the type of the variable.

For example:

let myVar = 100;
console.log(typeof myVar);   // number

myVar = "Hello World";
console.log(typeof myVar);   // string

myVar = {name: 'John Doe'};
console.log(typeof myVar);   // object

At this point, you might be thinking that you can directly use the typeof operator to check if the type of the variable is an Array. But, actually, it will not work.

If you use the typeof operator to check the type of an array, it will return you “object” not “array”. This is because originally everything is an object in JavaScript and the same thing is true for the arrays also.

let arr = [1, 2, 3, 4];
console.log(typeof arr);   // object

arr = []; 
console.log(typeof arr);   // object

In this article, I will show you three different ways to check if the variable is an array.


Check if a Variable is an Array using the isArray() Method

The Array.isArray() method takes a single parameter and checks if it is an array or not. If the passed value is an array, it returns a boolean value true, otherwise, it returns false.

let arr = [1, 2, 3, 4];
console.log(Array.isArray(arr));  // true

let str = "Hello World";
console.log(Array.isArray(str));  // false

let num = 10;
console.log(Array.isArray(num));  // false

let obj = {name: 'John Doe'};
console.log(Array.isArray(obj));  // false

The isArray() method does not depend on how you create the array. It always returns true whether you create the array using the array literal syntax([]) or using the Array() constructor.

See the following example:

let arr1 = [];
console.log(Array.isArray(arr1));  // true

let arr2 = [1, 2];
console.log(Array.isArray(arr2));  // true

let arr3 = new Array();
console.log(Array.isArray(arr3));  // true

let arr4 = new Array(1, 2, 3);
console.log(Array.isArray(arr4));  // true

let arr5 = Array(3);
console.log(Array.isArray(arr5));  // true

Check if a Variable is an Array using the instanceof Operator

The instanceof operator is used to check if a given variable is an instance of a given object.

Behind the scenes, the instanceof operator basically checks if the prototype of the given variable lies anywhere in the prototype chain of the given object. If it does, the instanceof operator returns true, otherwise, it returns false.

See the following example:

let arr = [1, 2, 3, 4];
console.log(arr instanceof Array);  // true

let str = "Hello World";
console.log(str instanceof Array);  // false

let num = 10;
console.log(num instanceof Array);  // false

Check using the Constructor Property of the Variable

The constructor property returns a reference to the constuctor function which created the object’s instance.

When you create an array whether using the Array() constructor or using the literal syntax i.e. [], it will always have the constructor property. The value of the constructor property for arrays will have a reference to the Array() constructor.

So, if we want to check if the variable is an array, we can check if the constructor property has a refernce to the Arrray() constructor or not. If yes, the variable is an array, otherwise not.

let arr1 = [1, 2, 3, 4];
console.log(arr1.constructor===Array);  // true

let arr2 = new Array(1, 2, 3);
console.log(arr2.constructor===Array);  // true

let str = "Hello World";
console.log(str.constructor===Array);  // false

Conclusion

In this article, we learned three ways to check if a variable is an Array using JavaScript.

The first approach is to use the Array.isArray() method. The isArray() method takes a single parameter and checks if the given value is an array. If it is an array, the isArray() method returns true, otherwise, it returns false.

The second approach is to check if the variable is an instace of the Array using the instanceof operator. If yes, the variable is an array, else not.

The third approach is to check the constructor property of the variable. If the constructor property of the variable has a reference to its Array() constructor, it’s an array, otherwise not.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.