Javascript isArray() : Check if an element/object is an Array

The isArray() function over at BreakingPar.com looks great at first, but unfortunately does not work in Safari. This one here does work πŸ˜‰

// Cross-Browser isArray(), including Safari
function isArray(obj) {
    return obj.constructor == Array;
}

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Join the Conversation

7 Comments

  1. Thank you for this, it was the quick fix I needed, and it sorted the afore mentioned Webkit issue that the BreakingPar.com version had.

    However, I found that if ‘obj’ was undefined. The code would break and an error would be returned.

    To fix this, I simply added to you code to safeguard it from this:

    function isArray(obj) {
    if (typeof obj != “undefined”) {
    return obj.constructor == Array;
    } else
    return false;
    }

    Hope this helps someone else!

    1. Or even simpler than shrewman example:

      function isArray(obj) {
      return obj && obj.constructor == Array;
      }

Leave a comment

Leave a Reply to Diogo Nechtan Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.