Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 258315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:10:13+00:00 2026-05-11T22:10:13+00:00

What is the best de-facto standard cross-browser method to determine if a variable in

  • 0

What is the best de-facto standard cross-browser method to determine if a variable in JavaScript is an array or not?

Searching the web there are a number of different suggestions, some good and quite a few invalid.

For example, the following is a basic approach:

function isArray(obj) {
    return (obj && obj.length);
}

However, note what happens if the array is empty, or obj actually is not an array but implements a length property, etc.

So which implementation is the best in terms of actually working, being cross-browser and still perform efficiently?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:10:14+00:00Added an answer on May 11, 2026 at 10:10 pm

    Type checking of objects in JS is done via instanceof, ie

    obj instanceof Array
    

    This won’t work if the object is passed across frame boundaries as each frame has its own Array object. You can work around this by checking the internal [[Class]] property of the object. To get it, use Object.prototype.toString() (this is guaranteed to work by ECMA-262):

    Object.prototype.toString.call(obj) === '[object Array]'
    

    Both methods will only work for actual arrays and not array-like objects like the arguments object or node lists. As all array-like objects must have a numeric length property, I’d check for these like this:

    typeof obj !== 'undefined' && obj !== null && typeof obj.length === 'number'
    

    Please note that strings will pass this check, which might lead to problems as IE doesn’t allow access to a string’s characters by index. Therefore, you might want to change typeof obj !== 'undefined' to typeof obj === 'object' to exclude primitives and host objects with types distinct from 'object' alltogether. This will still let string objects pass, which would have to be excluded manually.

    In most cases, what you actually want to know is whether you can iterate over the object via numeric indices. Therefore, it might be a good idea to check if the object has a property named 0 instead, which can be done via one of these checks:

    typeof obj[0] !== 'undefined' // false negative for `obj[0] = undefined`
    obj.hasOwnProperty('0') // exclude array-likes with inherited entries
    '0' in Object(obj) // include array-likes with inherited entries
    

    The cast to object is necessary to work correctly for array-like primitives (ie strings).

    Here’s the code for robust checks for JS arrays:

    function isArray(obj) {
        return Object.prototype.toString.call(obj) === '[object Array]';
    }
    

    and iterable (ie non-empty) array-like objects:

    function isNonEmptyArrayLike(obj) {
        try { // don't bother with `typeof` - just access `length` and `catch`
            return obj.length > 0 && '0' in Object(obj);
        }
        catch(e) {
            return false;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best approach to calculating the largest prime factor of a number?
Best recommendations for accessing and manipulation of sqlite databases from JavaScript.
Is it in best interests of the software development industry for one framework, browser
Are there any best practices (or even standards) to store addresses in a consistent
I know the Commons Validator framework is the de facto standard for Struts projects
What is the best way to access a Rails 3 REST-ful web service, developed
There doesn't seem to be any tried and true set of best practices to
How best to make the selected date of an ASP.NET Calendar control available to
For best performance, is it better to use a virtual IDE HDD or virtual
As best-behaved as I try to be about keeping my unit tests disconnected from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.