Check if javascript object is an Array


/ Published in: JavaScript
Save to your folder(s)

This will check if object is an array object.

This will add isArray() method to Array native object. Can be used to check if passed in object is an array. Future ecmascript 5 will have this included. Can use this in the meantime.

Using instanceof Array does not work in certain versions of IE.

e.g.

var arr = [1,2,3];

alert(Array.isArray(arr)); //displays as true


Copy this code and paste it in your HTML
  1. if(typeof Array.isArray==="undefined") {
  2. Array.isArray = function(arg) {
  3. return Object.prototype.toString.call(arg)=== "[object Array]";
  4. };
  5. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.