LA.NET [EN]

Sep 11

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];
alert(isArray(arr));


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];
alert(arr.toString());
alert(Object.prototype.toString.call(arr));


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.prototype.toString.call(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.

Leave a Reply

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


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>