Loading ...

JavaScript: How to check if a variable is an array or not | CodeAsp.Net

JavaScript: How to check if a variable is an array or not

 /5
0 (0votes)

jQuery provides a nice method isArray to check whether a variable is array or not. Below is the sampe code to use the isArray method:

    var item1={'key':'value'};
    var item2=[{'key':'value'}];
    var item3=[{'key':'value'},{'key2':'value3'}];
    var item4=new Array();
    var item5=[1,2,3];
    var item6={};

console.log('Is item1 an array: ',$.isArray(item1));
console.log('Is item2 an array: ',$.isArray(item2));
console.log('Is item3 an array: ',$.isArray(item3));
console.log('Is item4 an array: ',$.isArray(item4));
console.log('Is item5 an array: ',$.isArray(item5));
console.log('Is item6 an array: ',$.isArray(item6));



Output:

Live Demo: http://jsfiddle.net/raghav_khunger/X9ncL/2/embedded/result/

Comments (no comments yet)

Top Posts