Posted on 2006-04-10
Last Modified: 2012-08-13
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.
Question by:jstretch
    LVL 17

    Expert Comment

    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
    LVL 6

    Assisted Solution


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

    By Nab
    LVL 17

    Accepted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved." Ralph Waldo Emerson Introduction: One of the wonderful things about the web is that it makes it s…
    Article by: DanRollins
    This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now