How to check if a variable is an Array in JavaScript?

November 25, 2020 - 2 min read

To check if a variable is an array, there is more than one way to do it in JavaScript.

We can use:

Array.isArray() method

Consider this variable which is an array,

// variable which is an array
const nums = [2, 3, 4, 5];

Now let's check if the nums variable is an array by passing the variable as an argument to the Array.isArray() method like this,

// variable which is an array
const nums = [2, 3, 4, 5];

// check if nums variable is an array
// using the Array.isArray() method
const isIt = Array.isArray(nums);

console.log(isIt); // true ✅

See this example live in JSBin.

constructor property in the variable

Consider this variable,

// array of strings
const strArr = ["John", "Roy", "Lily"];

Now let's check if the strArr variable is an array using the constructor property in the variable like this,

// array of strings
const strArr = ["John", "Roy", "Lily"];

// check if strArr variable is an array
// using the constructor proerty in the variable
console.log(strArr.constructor === Array); // true ✅

See this example live in JSBin.

instanceof operator

Consider this variable,

// array of strings
const strArr = ["John", "Roy", "Lily"];

Let's check if the strArr variable is an array using the instanceof operator like this,

// array of strings
const strArr = ["John", "Roy", "Lily"];

// check if strArr variable is an array
// using the instanceof operator in JavaScript
console.log(strArr instanceof Array); // true ✅

See this example live in JSBin.

Feel free to share if you found this useful 😃.