Is it possible to detect if a variable is an array without using the length attribute or without using [0] accessors? I dont want to get a warning or error when doing this.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi jstretch,

Here is a solution, somebody had the same isArray() problem and wrote their own function:
I'll try and post an example of it.

Joe P

This is the code for checking array.
   var a = new Array()
  if(a.constructor.toString().indexOf("Array") != -1)

By Nab
Here is some sample code, the isArray() method is Kas Thomas's creation and solution for checking if an object is an array.  Just pass the method your object, it checks arguments[0] so if you pass nothing it does nothing, if you pass more then one object it will only check the first object.

[javascript code]
<script type="text/javascript">
function isArray()
  if (typeof arguments[0] == 'object')
    var criterion = arguments[0].constructor.toString().match(/array/i);
   return (criterion != null);  
  return false;

var array = new Array('Joe');
var str = "Joe2";
if (isArray(array))
if (isArray(str))

I put a bunch of alerts() so I could see what is going on and so I can explain to you what is happening.  First, look at the code outside of the function.  It creates an array and a string then uses the isArray() function (which returns true/false) and prints yes if it is an array or no if it is not.  Obviously this will do "yes" then "no".

I learned that when you create an object it shows the constructor used to initiate the object if you do (objectName).constructor.toString().  I learned that from his isArray() method which gets that string and in this case looks for the word Array.  Notice that when I create the Array I print the constructor, it indeed has Array, and the string constructor has String, NOT Array.  So that is simple enough.  Now for his isArray() function!

The isArray() does take params.  Not the normal way so don't be fooled.  It checks arguments[]s array, onlyt he first position [0].  Now comes the logic.  It first checks if the param is an object.  Fair enough, if you passed no argument it would be null and we would be done, it would then return false.  If you pass a param it will then check the constructor (as a string using toString()) and search using a regular expression (case insensitive) for the word array.  If found, it again makes sure the array is not actually null and returns true (or false if it was null).  Pretty brillient solution and I learned some intresting stuff.

Hope this works for you,
Joe P

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.