How To Check If A Variable Is An Array In JavaScript?

In JavaScript, an array is a special kind of object that holds an ordered collection of values. Sometimes, when working with JavaScript code, you may need to determine if a given variable is an array or not. There are several ways to achieve this, each with advantages and limitations. This article will explore different ways to check if a variable is an array in JavaScript.

Using the Array.isArray() method

The simplest and most straightforward way to check if a variable is an array in JavaScript is by using the built-in Array.isArray() method. This method returns true if the given variable is an array and false otherwise. Here's an example:

const myVar1 = [1, 2, 3];
console.log(Array.isArray(myVar1)); // true

const myVar2 = "hello";
console.log(Array.isArray(myVar2)); // false

One advantage of using Array.isArray() is that it is a built-in method, so it does not require any external libraries or additional code. Additionally, it works with any array, regardless of its creation method.

Using the Array.prototype property

Another way to check if a variable is an array in JavaScript is to use the Array.prototype property. This property is an object that represents the prototype for all array objects in JavaScript. We can use it to check if a variable is an array by using the Array.isArray() method with the call() function. Let's understand this operator in detail with an example:

const myVar1 = [1, 2, 3];
const myVar2 = "hello";

console.log(Array.prototype.call(myVar1) === myVar1); // true
console.log(Array.prototype.call(myVar2) === myVar2); // false

This method is not commonly used, but it is an option when other methods are unsuitable. One advantage is that it can be used with different versions of JavaScript.

Using the instanceof operator

Javascript comes bundled with instanceof operator, which can be used to verify if a specific variable is an array. This operator returns true if the given variable is an instance of the Array object and false otherwise. Let's take a look at an example.

const myVar1 = [1, 2, 3];
console.log(myVar1 instanceof Array); // true

const myVar2 = "hello";
console.log(myVar2 instanceof Array); // false

One thing that we need to keep in mind is that the instanceof operator can sometimes give false positives. For example, if an object is created using the Array constructor but with a different name, the instanceof operator will still return true. For this reason, it's generally recommended to use the Array.isArray() method instead

Using the Object.prototype.toString() method

Another way to check if a variable is an array is by using the Object.prototype.toString() method. This method returns a string that represents the object's type. The string will be "[object Array]" if the variable is an array. Here's an example:

const myVar1 = [1, 2, 3];
console.log(Object.prototype.toString.call(myVar1) === "[object Array]"); // true

const myVar2 = "hello";
console.log(Object.prototype.toString.call(myVar2) === "[object Array]"); // false

While this method works, it's more verbose than the previous two methods and is generally not recommended for checking if a variable is an array.

In conclusion, multiple ways exist to check if a variable is an array in JavaScript. The most recommended way is to use the Array.isArray() method as it's simple, straightforward, and doesn't give false positives. However, the instanceof operator, prototype, and the Object.prototype.toString() method can also be used in certain situations.


Similar Articles