Arrays in JavaScript

If you’ve been doing programming for some time, you’ve probably used arrays. Traditionally, arrays are linear allocations of memory where elements are accessed through indexes. In other words, in “traditional” programming languages, when you create an array, you’re allocating a chunk of contiguous bytes which is big enough to save the specified number of elements. Then, you can access each element by using an integer which indicates the position of that element.

When you start using a language, you’ll probably have to look at the docs which describe its API. In this case, you’ll probably find a reference on JavaScript’s Array object. Unfortunately, arrays in JavaScript don’t really comply with the definition I’ve presented in the previous paragraphs. The truth is that arrays are just objects which lets you access its properties through an integer which is used as a subscript. This is the main reason why array access in JavaScript is way slower than it should if the language supported “real arrays” and that’s why you can use the traditional for(…in …) to enumerate all the elements of the array.

The advantage of using the Array object is that you end up inheriting several useful methods (ex.: splice). Here’s a small snippet that shows how to use an array in JavaScript:

var arr = new Array();
arr.push(1);
arr.push(2);
alert(arr.length);

We start by creating a new Array through its “constructor”. We then add two elements and use its length property to get the total number of slots in the array.

As you can see, this syntax is a little verbose for creating new arrays. In fact, there’s another option: we can use literal arrays. You can see this feature as the equivalent of literal objects (when compared with using functions to create a new objects). Here’s how you’d rewrite the previous example with literal arrays:

var arr = [1,2];
alert(arr.length);

Much simpler,right?

It’s important to keep in mind that the length property is read/write. What this means is that you, for instance set it to a specific value. When you do this, you’ll be increasing or decreasing the “size” of the array. When you decrease the “size”, you end up  deleting the elements that fall out of the “current size”. For instance, notice what happens when you run the next snippet:

var arr = [1,2];
arr.length = 1;
for (var a in arr) {
    alert(arr[ a ]);
}

By running the previous snippet, you’ll end up with an array that has only one element (in the previous case, the value 1). Notice that the length property won’t really tell you the number of items that are stored on an array. For instance, take a look at the following snippet:

var arr = [];
arr[100] = 10;
alert(arr.length);

When you run the previous snippet, you’ll end up getting the value 101. Why? Well, that happens because when you pass the index (arr[100]), you end up influencing the value of the length property. The moral of the story is simple: if you pass an integer to the [] operator that is bigger than the current length’s value, you’ll  end up updating the value of the length property to that value + 1. Notice that this doesn’t mean you’re allocating space for the new elements. You’re only changing the current value of the length property.

And that’s it for today. Stay tuned for more on JavaScript.

Advertisements

~ by Luis Abreu on September 9, 2009.

2 Responses to “Arrays in JavaScript”

  1. you can initialize array elements in the constructor
    var arr = new Array(1,2);
    also a small gotcha
    IE does not support arr.indexOf(obj) where other browsers do.

  2. I think your blog”s security system mangled the end of the third paragraph.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: