Check that a variable is an Array:-
var data = [21,56,23,48,19], obj={};
Using a function:
function isArray(x) {
return x.constructor.toString().indexOf("Array") > -1;
}
isArray(data);// true
Using a method of ES 5: isArray() method of ES5
returns true if an variable is an array else false.isArray(data);// true
Using a method of ES 5: isArray() method of ES5
var status= Array.isArray(data);
console.log(status) //true
var status= Array.isArray(obj);
console.log(status) //false
Using a instanceof operator: instanceof operator
returns true if an variable is created with a constructor else false.
data instanceof Array; // return true
obj instanceof Array; // return false
Comments
Post a Comment