Associative arrays and JavaScript – Part 5

I’m sure I don’t have to tell you that one of the more useful object classes in JavaScript is the Array class, which I like to think of as being a kind of subspecies of a larger (but formally nonexistent) String-Array superclass. That is, I like to think in terms of the Array class having available to it all the methods of both the Array and String classes. This way of thinking makes arrays even more versatile and powerful, because everything you can do to a string, you can do to an array, and vice versa (when you think this way). The reason I feel justified in thinking this way is that it’s trivially easy, in JavaScript, to convert back and forth between strings and arrays. The Array class (like just about every JavaScript class) has a built-in toString() method that instantly converts any array to the equivalent string. But before we get into that, try this mental exercise: See how many ways you can think of to create an array in JavaScript.

Let me give you some hints. (Stay with me here as we’ll soon be getting into some bizarre territory.) Let’s start with the usual methods first.

var empty = new Array(); // create empty array the formal way
var evens = new Array(2,4,6); // pre-initializedvar primes =
[7,11,13]; // array literal
var mixed = [ true,7,global,new
Function(‘{return Math.pow(2,arguments[0]);}’) ]; // array literal

That last one may seem strange. It’s perfectly legal, though. You can include objects (such as the Global object) in an array, and you can include functions, including (as here) an anonymous function. The example function shown here calculates 2 to whatever power you pass in as an argument. Thus you can later do:

var n = mixed[3](9); // ‘n’ is now 512

Note that arrays are zero-based in JavaScript (as in C and other ‘respectable’ languages). That means the ‘mixed’ array contains ‘true’ in the zero position and a function pointer in position three.

We’re not done yet thinking up ways to create arrays, of course. For example, you can preallocate an empty array with a given number of slots, to be filled later:

var pre = new Array(128); // array with 128 empty slots

When I say ’empty,’ I don’t mean that the array positions contain zero or null. They are actually undefined, until you assign values to them.

Note carefully that in the example just given, we did NOT create a one-element array with ‘128’ as the first (er, zeroth) element. We created an array with 128 undefined elements. To create a one-element array with 128 in the first position, you’d want to do new Array(‘128’). I.e., make the argument into a string first. (It’ll behave like a number later, if you start using arithmetic on it.)

Have we forgotten any ways to create arrays? I can think of at least one or two. I’ll tell you one today and leave the more interesting example for tomorrow. (Maybe you can guess it by then.) Here is a sneaky way to create an array:

var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');

Remember before when I said that it’s possible to think of the Array and String classes as constituting one big superclass? Well, here’s an example of what I mean. You can create an array from any string using the split() method of the String class. This method
takes one argument: the delimiter you want to ‘split’ on. If you specify no
argument here, you’ll just get back the original string, but if you specify the
null string, you’ll get back an array containing every individual character. In
the above case, we created a 26-element array containing the letters of the
English alphabet.

You May Also Like

About the Author: Kas Thomas

Leave a Reply