Check if a Variable is an Array in JavaScript Example

JavaScript check if variable is array or not; In this tutorial we will show you How to Check If a Variable is an Array or not in JavaScript.

The Javascript isArray()” method to check whether the element is array or not. jQuery. isArray() returns a boolean value that indicates whether the object is a JavaScript array or not.

To check whether an object (or a variable) is an array or not in javascript, we can use two methods the Array.isArray() method or the instanceof operator. These both methods returns true if the value is an array; otherwise returns false.

Method 1: Check Using the isArray method

The Array.isArray() method checks whether the passed variable is an Array object and return true, Otherwise it will returns false. The isArray syntax looks like this:



The following example show if variable is an array or not using the Array.isArray() method:


let colors = ['red','green','blue'];

let isArray = Array.isArray(colors);
console.log(isArray); // true;

Method 2: Check Using the 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 an the given variable has a prototype of ‘Array’.


variableName instanceof Array

The following operator returns true if the variableName is an array and false if it is not. Check the following example below with output.


const ratings = [1, 2, 3, 4, 5];
const vote = { user: 'John Doe', rating: 5 };
const str = "It isn't an array";

console.log(ratings instanceof Array); // true
console.log(vote instanceof Array); // false
console.log(str instanceof Array); // false

JavaScript is best known for web page development and these both methods are supported in all major browsers, such as Chrome, Firefox, IE (9 and above), etc. For more follow us on social networks.

Leave a Comment