| Viewed 52 times

How to check if a variable is an array in JavaScript


How to check if a variable is an array in JavaScript

1 Answer
Mikdam Qandeel
Answered
0

There are a lot of Array methods, one of them is isArray() method.

If you want to check if your variable is an Array or NOT you have to use isArray() method.

This function returns true if the variable is an array, and false if not.

Example:

var testArray = ["Qandeel","Academy"];

console.log(Array.isArray(testArray));

Output:

true

Another way to check is by using var.constructor === Array.

Example:

var testArray = [1, 2, 3];

console.log(testArray.constructor === Array);

Output:

true