JavaScript Check if Array Tutorial

In this section we will learn how to check an object and see if it’s an array with the help of isArray() method.

JavaScript isArray() Method

Sometimes we want to know if an object is an array or not and, based on the result, run a set of instructions. So in situations like this we can use the static method `isArray()`.

Array isArray() Syntax:

Array.isArray(obj)

Array isArray() Method Parameter

The method takes one argument and that is the reference to an object that we want to know if it’s an array or not.

Array isArray() Method Return Value

The return value of this method is a Boolean.

If the value was “true” that means the target object is in fact an array.

But if the value was “false” that means the target object is not an array.

Example: is array JavaScript

const arr = ["John","Doe","Jack","Omid","Ellen"];
const arr2 = ["Red","Blue","Green"];
const arr3 = ["Apple","Google","Microsoft"];
const obj = {
  fullName: "John Doe"
}
const obj2 = {
  fullName: "Omid Dehghan"
}
console.log(`The 'arr' ${(Array.isArray(arr)? 'is':"isn't")} an array.`);
console.log(`The 'arr2' ${(Array.isArray(arr2)? 'is':"isn't")} an array.`);
console.log(`The 'arr3' ${(Array.isArray(arr3)? 'is':"isn't")} an array.`);
console.log(`The 'obj' ${(Array.isArray(obj)? 'is':"isn't")} an array.`);
console.log(`The 'obj2' ${(Array.isArray(obj2)? 'is':"isn't")} an array.`);

Output:

The 'arr' is an array.

The 'arr2' is an array.

The 'arr3' is an array.

The 'obj' isn't an array.

The 'obj2' isn't an array.
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies