While working with Javascript, you may encounter situations where you need to determine whether a variable is an array or not. There are different ways to achieve this.
In this blog post, we'll look into different ways to check if a variable is an array in JavaScript. You will also see which method is the best and why.
There are 4 techniques to check whether a variable is an array or not. These are:
- Using
Array.isArray()
Method. - Using the
instanceof
Operator. - Using the
constructor
Property. - Using
Object.prototype.toString.call()
Method.
Let's discuss each of these techniques in detail with examples.
Check If a Variable is an Array Using Array.isArray() Method
To check if a variable is an array in JavaScript, you can use the Array.isArray()
static method. This method accepts one argument and returns true
if it is an array. Otherwise, this method returns false
.
const numbers = [3, 4, 5, 6, 7, 8, 9, 10]
const username = 'john_doe'
const user = {
name: 'John Doe',
age: 13,
}
console.log(Array.isArray(numbers))
// true
console.log(Array.isArray(username))
// false
console.log(Array.isArray(user))
// false
Here I have 3 variables numbers
, username
, and user
which are array, string, and object respectively. I am checking each of these variables using the Array.isArray()
method to see whether it can determine the array correctly or not.
As you can see, when you pass numbers
variable to this method it returns true
because it is an array. But for other variables, it returns false
because they are not arrays.
Also Read: Conditionally Define or Add Elements to a JavaScript Array
Check If a Variable is an Array Using instanceof Operator
You can use the instanceof
operator to check if each variable is an instance of the Array
object. The instanceof
operator returns true
if an object is an instance of a specified object type; otherwise, it returns false
.
const numbers = [3, 4, 5, 6, 7, 8, 9, 10]
const username = 'john_doe'
const user = {
name: 'John Doe',
age: 13,
}
console.log(numbers instanceof Array)
// true
console.log(username instanceof Array)
// false
console.log(user instanceof Array)
// false
When you will use instanceof
operator to check whether the numbers
variable is an instance of the Array
object, it returns true
. It means this variable is an array.
But if you do the same this with other variables, it will return false
because these are not arrays.
Also Read: Remove Duplicates From a JavaScript Array Like a Pro (Easy)
Check If a Variable is an Array Using constructor Property
You can use the constructor
property to check if a variable is an array. The constructor
property is a reference to the constructor function that created the object.
For example, if you create an object using a Person
constructor function, the constructor
property return Person
as its value.
Similarly, if you create an array using the []
syntax or the Array()
constructor function, this property of the array will return Array
function.
You can use this return value to validate the type of a variable. If you access this property on a variable and you get Array
as the return value, it means that the variable is an array.
const numbers = [3, 4, 5, 6, 7, 8, 9, 10]
const username = 'john_doe'
const user = {
name: 'John Doe',
age: 13,
}
console.log(numbers.constructor === Array)
// true
console.log(username.constructor === Array)
// false
console.log(user.constructor === Array)
// false
When I access the constructor
property on the numbers variable and check whether the return value is equal ( === ) to Array
, I get true.
If I perform the same check for the other two variables, I get false for both. Because these are not arrays.
Also Read: How to Split Paragraphs into Sentences Using JavaScript
Validate Arrays Using Object.prototype.toString.call() Method
the Object.prototype.toString.call()
method returns the string representation of an object's internal class. This string value contains the object's class name inside square brackets, such as [object Array]
for an array.
You can use this method to determine whether the type of a variable is an array or not by comparing the return value with [object Array]
using the strict equality ( === ) operator.
const numbers = [3, 4, 5, 6, 7, 8, 9, 10]
const username = 'john_doe'
const user = {
name: 'John Doe',
age: 13,
}
console.log(Object.prototype.toString.call(numbers) === '[object Array]')
// true
console.log(Object.prototype.toString.call(username) === '[object Array]')
// false
console.log(Object.prototype.toString.call(user) === '[object Array]')
// false
When I pass these 3 variables to this method and compare the return values with [object Array]
, it only returns true
for the numbers
variable. For the other 2 variables, I get false
.
It's worth noting that this technique is more reliable than using the instanceof
operator to determine the type of a variable. Because the instanceof
operator can sometimes produce unexpected results.
Also Read: JavaScript Objects in localStorage: Storing & Getting [Tips]
Which is The Best Way to Validate an Array in JavaScript?
We have discussed 4 techniques in this blog post to check whether a variable is an array or not. All of them are reliable and will work for this purpose.
However, using Array.isArray()
method is the most recommended way for validating your JavaScript array. Because it's a built-in method in that JavaScript Array
object that is specifically designed to check if the variable is an array.
It is also faster, concise, and easy to use compared to other techniques. You will get better support for modern browsers and environments because it's a newer method.
Also Read: Store and Get JavaScript Arrays in localStorage [Many Ways]
Why Can't We Use typeof Operator
Normally if you want to check the type of a value or variable, you use the typeof
operator in JavaScript. But to check if a variable is an array, you can't use this operator.
Because the array in JavaScript is not a primitive type. If you use typeof
operator for an array it will return object
type instead of array
type.
const numbers = [3, 4, 5, 6, 7, 8, 9, 10]
const username = 'john_doe'
const user = {
name: 'John Doe',
age: 13,
}
console.log(typeof numbers)
// object
console.log(typeof username)
// string
console.log(typeof user)
// object
When you use this operator for the numbers
variable, it returns object
as its type even though you know it's an array. It also returns object
for the user variable which is an actual object.
As you can see there is no way to differentiate the types of these two variables. That's why we can't use this operator to find the type of an array.
typeof 'Web Mound'; // 'string'
typeof 35; // 'number'
typeof true; // 'boolean'
typeof false; // 'boolean'
typeof function() {}; // 'function'
typeof {}; // 'object'
typeof []; // 'object'
The typeof
is useful for determining the primitive data types in JavaScript (such as strings, numbers, booleans, etc.), you can't use it to determine if a variable is an array or an object.
Also Read: 6+ Ways to Find an Object in an Array of Objects Using JavaScript
Conclusion
Checking the type of a variable is a very common task in JavaScript. You may want to perform a different action depending on the type of variable.
That's why I have shown you multiple ways to make sure that a variable is an array before doing something with it. You can apply any one of those in your JavaScript code.
But Array.isArray()
and Object.prototype.toString.call()
are considered to be the most reliable methods for determining whether a variable is an array, while instanceof
and the constructor
property have some limitations.