// inside if else check
if(Array.isArray(myVarToTest)) {
// myVatToTest is an array
} else {
// myVarToTest is not an array
}
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
Array.isArray([]) //true
Array.isArray({}) //false
Array.isArray('') //false
Array.isArray(null) //false
Array.isArray(yourItem)
if(Array.isArray(colors)){
//colors is an array
}
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
// npm i is-js-array
const { isJSArray } = require("is-js-array");
isJSArray([]); // true
isJSArray({}); // false
if(Object.prototype.toString.call(someVar) === '[object Array]') {
alert('Array!');
}