Arrays in JavaScript – part III

Today we’re going to talk a little more about arrays in JavaScript. In previous posts, we’ve already seen that JavaScript doesn’t really support “true” arrays; instead, it introduces an object which behaves like an array. This means that you can use integers as keys for accessing values (internally, the object will convert the integer into a string before accessing that property).

This means that you might end up getting into some trouble if you need to check if a specific variable is an array. Since arrays are objects, that means that typeof will always return the string “object”. hum…not really something that will work here, right? However, there’s no doubt that if a variable is an array, then it will have been created through the Array constructor. And this gives us our first option for checking if a variable is an array:

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

And now, this should work (notice that we start by checking for truthy values; passing null to the isArray function won’t simply work):

var arr = [1, 2, 3];

It should, but it doesn’t. Ok,let me be clear about this: it won’t work if the array is defined in a different frame. Hum…it looks like we need something better…

I guess we all know that arrays have a length property which returns a number. Lets try using that:

function isArray(par) {
    return par &&
           typeof par.length === "number";

And now,our previous tests pass. Unfortunately, this test fails (we get a false positive) when we pass an object which has a length property. So, it looks like we need to do more. One option is using the Object’s toString method. Take a look at the following snippet:

var arr = [1, 2, 3];

The first alert message will print the elements of the array. That happens because the Array object changes the default implementation of the toString method which it “inherits” from the Object “type”. When you have an object and call toString, you’ll get a string on the form “[object Constructor]”.  For instance, {name: "luis"}.toString() will return “[object Object]” because we’re creating a literal object (and that means we’re using the Object constructor).

So, what we need to do is use the “base” toString method and run it “over an instance of an array”. The good news is that we already now how to do that (remember our old call and apply function methods). So, here’s some code that can really check for an array:

function isArray(par) {
    return par &&
  === "[object Array]";

To my knowledge, this is the best option for testing for an array (it’s simple and it works). btw, this is how JQuery implements its own isArray helper function. Stay tuned for more on JavaScript.


~ by Luis Abreu on September 11, 2009.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: