// inside if else check
if(Array.isArray(myVarToTest)) {
// myVatToTest is an array
} else {
// myVarToTest is not an array
}
Array.isArray([]) //true
Array.isArray({}) //false
Array.isArray('') //false
Array.isArray(null) //false
let fruit = 'apple';
let fruits = ["apple", "banana", "mango", "orange", "grapes"];
const isArray = (arr) => Array.isArray(arr);
console.log(isArray.(fruit)); //output - false
console.log(isArray.(fruits)); //output- true
function isArray(value) {
return Object.prototype.toString.call(value) === "[object Array]";
}
Method 1: Using the isArray method
Array.isArray(variableName)'
Method 2:
variable instanceof Array
Method 3:
variable.constructor === Array