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

This article will show you how to check whether the given variable value is an array or not.

Table of Content

  • Using JavaScript isArray() method
  • Using JavaScript instanceof operator
  • Checking the constructor property of the variable
  • Using Object.prototype.toString.call()
  • Checking the Length Property
  • Using Array.prototype.isPrototypeOf()

Using JavaScript isArray() method

isArray() method checks whether the passed variable is an Array object or not. 

Syntax:

Array.isArray(variableName)

It returns a true boolean value if the variable is an array and a false if it is not.

Example 1: In this example, we will check if a given variable is an array or not using the isArray() method in JavaScript. 

Javascript
function checkArray() {
    let str = 'This is a string';
    let num = 25;
    let arr = [10, 20, 30, 40];

    let ans = Array.isArray(str);
    console.log("Output for String: " + ans);

    ans = Array.isArray(num);
    console.log("Output for Number: " + ans);

    ans = Array.isArray(arr);
    console.log("Output for Array: " + ans);
}

checkArray();

Output
Output for String: false
Output for Number: false
Output for Array: true

Using JavaScript instanceof operator

The instanceof operator is used to test whether the prototype property of a constructor appears anywhere in the prototype chain of an object. This can be used to evaluate if the given variable has a prototype of ‘Array’.

Syntax:

variable instanceof Array

Return value: The operator returns a true boolean value if the variable is the same as what is specified (here an Array) and a false if it is not. This is shown in the example below. 

Example: In this example, we will check if a given variable is an array or not using the instanceof operator in JavaScript. 

Javascript
function checkArray() {
    let str = 'This is a string';
    let num = 25;
    let arr = [10, 20, 30, 40];

    let ans = str instanceof Array;
    console.log("Output for String:" + ans);

    ans = num instanceof Array;
    console.log("Output for Number:" + ans);

    ans = arr instanceof Array;
    console.log("Output for Array:" + ans);
}

checkArray();

Output
Output for String:false
Output for Number:false
Output for Array:true

Checking the constructor property of the variable

The constructor property is the another method to check a variable is an array by checking its constructor with Array. 

Syntax:

variable.constructor === Array

This becomes true if the variable is the same as what is specified (here an Array) and false if it is not. This is shown in the example below. 

Example: In this example, we will check if a given variable is an array or not by checking the constructor property of the variable.

Javascript
function checkArray() {
    let str = 'This is a string';
    let num = 25;
    let arr = [10, 20, 30, 40];

    let ans = str.constructor === Array;
    console.log("Output for String:" + ans);

    ans = num.constructor === Array;
    console.log("Output for Number:" + ans);

    ans = arr.constructor === Array;
    console.log("Output for Array:" + ans);
}

checkArray();

Output
Output for String:false
Output for Number:false
Output for Array:true

Using Object.prototype.toString.call()

The Object.prototype.toString.call() method can be utilized to determine the type of an object. If the result of this method call is ‘[object Array]’, then the variable is an array.

Syntax:

Object.prototype.toString.call(variableName) === '[object Array]'

Example:

JavaScript
let str = 'This is a string';
let num = 25;
let arr = [10, 20, 30, 40];

let strResult = Object.prototype.toString.call(str) === '[object Array]' ? 'true' : 'false';
console.log("Output for String: " + strResult);

let numResult = Object.prototype.toString.call(num) === '[object Array]' ? 'true' : 'false';
console.log("Output for Number: " + numResult);

let arrResult = Object.prototype.toString.call(arr) === '[object Array]' ? 'true' : 'false';
console.log("Output for Array: " + arrResult);

Output
Output for String: false
Output for Number: false
Output for Array: true

Checking the Length Property

You can determine if a variable is an array by checking if it has a numeric length property. Non-array objects won’t have this property, or if they do, it won’t represent the number of elements in a collection.

JavaScript
// Example:
var variable = [1, 2, 3];

if (typeof variable === 'object' && variable !== null && !isNaN(variable.length)) {
  console.log('The variable is likely an array.');
} else {
  console.log('The variable is not an array.');
}

Output
The variable is likely an array.

Using Array.prototype.isPrototypeOf()

The Array.prototype.isPrototypeOf() method checks if a variable is an array by determining if Array.prototype exists in the prototype chain of the variable. If it does, the method returns true, confirming the variable is an array.

Example: In this example we checks if variable and anotherVariable are arrays by testing if Array.prototype is in their prototype chains, then logs the results. variable is an array, so isArray is true, and isAnotherArray is false.

JavaScript
const variable = [1, 2, 3];
const isArray = Array.prototype.isPrototypeOf(variable);
console.log(isArray); 

const anotherVariable = { a: 1, b: 2 };
const isAnotherArray = Array.prototype.isPrototypeOf(anotherVariable);
console.log(isAnotherArray); 

Output
true
false


JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.



Contact Us