How do we check if an object is an array in Javascript?


Array is a data type which can store multiple elements of similar data types. For example, if an array is declared as integer data type then it stores one or more elements of the integer data type.

To check if the given object or to know the data type, we can use directly the typeof() method in JavaScript.

Using typeof() method

typeof() is a function which gives the type of the given object. The typeof() will return a string which is the data type of the given operand. The operand can be an object, a variable or a function. For all of those it returns the type accordingly.

Syntax

The syntax of typeof() is as follows.

Typeof(operand) // operand can be a function, object or a variable.

Now, we will move onto understanding the working of this method.

Example 1

This example demonstrates how the typeof() is used to get the datatype of the operand −

function getType(){ str = "Abdul Rawoof" arr = [1,3,5,8] num = 90 set1 = ([1,265,890,22]) dict = {a:1,b:2} console.log("The input is: ",str," and the type is: ",typeof(str)) console.log("The input is: ",arr," and the type is: ",typeof(arr)) console.log("The input is: ",num," and the type is: ",typeof(num)) console.log("The input is: ",set1," and the type is: ",typeof(set1)) console.log("The input is: ",dict," and the type is: ",typeof(dict)) } getType()

But, when the typeof() function is used, it will not return if it is an array as “Array” rather it returns as “Object”. So to overcome this, other methods are used.

Using isArray()

isArray() is a function which returns as “array” if the given input is an array. It is only used to know if the given input is array or not, unlike the typeof() which gives the data type of the given input.

This function returns “true” if the given input is an array and “false” if it not.

Syntax

The following is the syntax of isArray() method.

Array.isArray(object)

Object here is a parameter for isArray() and it can be an array, string, set, map etc.

Example 2

This example demonstrates the usage of isArray() function to check for array −

<!DOCTYPE html> <html> <head> </head> <body> <p> checking given input isx array with isArray() function </p> <script> dict = {name: "John", age: 18}; arr = ["red", "green", "blue", "yellow"]; arr1 = [1, 2, 3, 4, 5]; null1 = null; document.write("Given input is "+ dict + " is an array :" + Array.isArray(dict) + "<br>"); document.write("Given input is "+ arr + " is an array :"+Array.isArray(arr) + "<br>"); document.write("Given input is "+ arr1 + " is an array :"+Array.isArray(arr1) + "<br>"); document.write("Given input is "+ null1 + " is an array :"+Array.isArray(null1)); </script> </body> </html>

Using the Constructor

You can use arr.constructor === Array to determine whether an object is array. This doesn’t work for all objects though.

// This will fail: console.log(undefined.constructor === Array) // This will fail: console.log(null.constructor === Array) console.log("".constructor === Array) console.log({}.constructor === Array) console.log([].constructor === Array) console.log([1, "hello"].constructor === Array) console.log(new Array().constructor === Array)

Updated on: 26-Aug-2022

170 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements