xxxxxxxxxx
// Check if something is an Array
// just like you do with "typeof"
Array.isArray([1, 2, 3]); // true
Array.isArray('asdf'); // false
xxxxxxxxxx
let names=['Jhon','David','Mark'];
console.log(Array.isArray(names));
// true
let user={id:1,name:'David'};
console.log(Array.isArray(user));
// false
let age 18;
console.log(Array.isArray(age));
// false
xxxxxxxxxx
var colors=["red","green","blue"];
if(Array.isArray(colors)){
//colors is an array
}
xxxxxxxxxx
Array.isArray([]) //true
Array.isArray({}) //false
Array.isArray('') //false
Array.isArray(null) //false
xxxxxxxxxx
if(Array.isArray(myVarToTest)) {
// myVatToTest is an array
} else {
// myVarToTest is not an array
}
xxxxxxxxxx
Method 1: Using the isArray method
Array.isArray(variableName)'
Method 2:
variable instanceof Array
Method 3:
variable.constructor === Array
xxxxxxxxxx
let variable1 = [2, 3]
let variable2 = "test";
variable1.constructor === Array; // true
variable2.constructor === Array; // false
xxxxxxxxxx
function isArray(value) {
return Object.prototype.toString.call(value) === "[object Array]";
}