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([1, 2, 3]); // true
Array.isArray('asdf'); // false
if(Array.isArray(colors)){
//colors is an array
}
if(Array.isArray(myVarToTest)) {
// myVatToTest is an array
} else {
// myVarToTest is not an array
}
let variable1 = [2, 3]
let variable2 = "test";
variable1.constructor === Array; // true
variable2.constructor === Array; // false
// Check if something is an Array
// just like you do with "typeof"
Array.isArray([1, 2, 3]); // true
Array.isArray('asdf'); // false
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
// I know javascript can be hard but see this example and you will learn
// Javascript if condition and array example
var people = ["filex", "alex", "jon"];
var peopleNeeded = people[1]; // 1 is the index bc the index starts from 0
if (peopleNeeded <= people[1]) {
console.log("alex needed");
} else {
console.log("lol");
}
if(Object.prototype.toString.call(someVar) === '[object Array]') {
alert('Array!');
}