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:

  1. 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".
  2. var arr = {};
    if(arr.isArray){
         console.log('Object holds array');
    }
    else{
         console.log('Object is not array');
    }
    It return "Object is not array".
  3. 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".
  4. var arr = [];
    if(arr.isArray){
         console.log('Object holds array');
    }
    else{
         console.log('Object is not array');
    }
    It return "Object holds array".
  5. 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".
  6. 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

MyJs is a collection of java script extension methods that enhances the JavaScript objects, class object, DOM element, domElementsCollections, Date, String, Number and other data type. 

Include in you project landing page (master page) and it will extends your DOM and JavaScript Objects.

Comments

Popular posts from this blog

javascript : String.right

javascript : String.left

javascript : .nextAll()