JavaScript Program to Check If an Item is an Array

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

Many times in our Web Development Journey or itself in the JavaScript language, we may encounter cases where we are given a list of values or data, but before we use them, we first would need to verify whether the given data is an array, is an object or is entirely some different form of data. Therefore, the JavaScript language provides us with various pre-defined methods using which we can verify whether a given item is a JS array or not.

Introduction

JavaScript language is the brain of the Web. It is the language responsible for interactivity that we see on websites today.

Although this is not a JavaScript introduction article, hence I will not be talking a lot about that, but I do want to mention that all, or let's say most of the web pages we see today, are dealing with some data. Now that data might be coming from a database, an API call, or maybe from some other way, but at the end of the day, the websites and the developers are dealing with some data.

On a high level, we can categorize that data into two divisions; first, that data can be in an object form, and second, the data can be in an array form.

And if, in some cases, we need to call some function or do some specific operation, if a given data or some received data is in the array form, then how can we do so? Only if we knew how to determine that our data is an array, in this article, we would be seeing several ways using which we can check if a given data or list of values is an array or not.

JavaScript Program to Check If an Item is an Array

Using the Array.isArray() Method

The Array.isArray() method in JavaScript takes one parameter and determines whether the passed value is an array. It returns true if the value given is an array; else returns false.

Output

Using the instanceof Operator

The instanceof operator in JavaScript is used to test whether the prototyping property of a constructor; here, in this case, the Array constructor appears anywhere in the prototyping chain of the object, here the arr1, arr2, notArr1, notArr2 etc. are the objects. The operator returns true if it's found; else returns false.

Output

By Checking the Constructor Property of the Variable

We can also check whether a given list is an array or not with the help of its constructor property. When we call the constructor property on an object, it returns a reference to the constructor function which created that specific object, using which we can verify if that given list is an array or not.

Output

Here also, we can see the result came as expected. But one thing that we should notice is that when we used the constructor property on the null type, it returned TypeError; this is because the null in JavaScript means the absence of any sort of value, meaning the constructor property has nothing to check in there, there is no value at all inside it, this is why it returned with an error.

Conclusion

  • While writing programs, we may encounter cases where we have to check if a given list of values is an array or not.
  • For that, the JavaScript language provides us with various pre-defined methods using which we can accomplish this task.
  • Methods like isArray(), the instanceof operator, the constructor property, etc., can be used for the purpose.
  • These methods either take a data list as a parameter or operate on one and return true or false based on whether the list is an array or not, respectively.