javascript : Object.isArray
javascript : Object.isArray
Target:
Applicable with any javascript Object Variable that holds array value .
Description :
When an object holds an array then it return true otherwise undefined. basically it is defined in array prototype. Many time in programming we face a problem that an object variable holds array value or other object structure then it help us. Before writing the code of array manipulation we can check whether a variable holds array value or not.
Note: It is an property of array that is defined in Array Prototype.
It is useful to check an object variable is array or any other type.
When we check the typeof(object_variable) it return 'object' not 'array'.
Syntax:
Object.isArray
return true when object hold's array value otherwise undefined
Usase:
- var arr = [1,2,3,4,5];
if(arr.isArray){
console.log('Object holds array');
}
else{
console.log('Object is not array');
}
It return "Object holds array". - var arr = {};
if(arr.isArray){
console.log('Object holds array');
}
else{
console.log('Object is not array');
}
It return "Object is not array". - var arr = document.querySelectorAll('div');
if(arr.isArray){
console.log('Object holds array');
}
else{
console.log('Object is not array');
}
It return "Object is not array". - var arr = [];
if(arr.isArray){
console.log('Object holds array');
}
else{
console.log('Object is not array');
}
It return "Object holds array". - var arr = {arr1:[1,2,3],arr2:[4,5,6]};
if(arr.isArray){
console.log('Object holds array');
}
else{
console.log('Object is not array');
}
It return "Object is not array". - var arr = [{name:'ram',age:38},{name:'mohan',age:37}];
if(arr.isArray){
console.log('Object holds array');
}
else{
console.log('Object is not array');
}
It return "Object holds array".
Download MYJS.js
Comments
Post a Comment