Home > Articles

Chapter 3: Array()

  • Print
  • + Share This


Arrays, simply put, are objects that can have a series of properties ordered by an index number. Sometimes it matters to have a specific order to these properties, called elements of the array. Sometimes it does not, except to give you a grouping of common elements. (Do not confuse these elements with HTML elements, discussed in Part III, "Document Object Model for HTML Documents.")

Arrays are among the most useful features in JavaScript, simply for their flexibility. In the preceding two chapters, you saw a few examples of them. You will continue to see arrays throughout this book, in many of the examples. You will also see a few properties within this book that are special instances of Array(), as generated from strings and regular expressions.

Numbered and Unnumbered Properties of Arrays

Suppose you have an object myArray which is an array. Elements of the myArray object, the numbered properties, you can reach by referring to myArray[x], where x is the index number of the array indicating which element you want to reach.

(Only nonnegative integers less than 232 - 1 qualify as index numbers for an array's elements.)

The first element of myArray is myArray[0]. The second is myArray[1]. The nth array element is myArray[n-1]. This is true for all arrays: they begin their element list at 0. The element list for myArray is the collection of all numbered elements, from myArray[0] to myArray[myArray.length-1]. The element list for any array object is defined similarly.

You can always add members to an array's element list. The most common way to do so is to refer to the length property of the array as an element list index number. (The length property, as I will note later in this chapter, is tied to the number of elements in the array; changes to one will reflect in the other.) For example:

myFunc = new Array()
// myFunc.length == 0
myFunc[myFunc.length] = new Object() // myFunc[0] is now an object
// myFunc.length == 1
myFunc[myFunc.length] = new Object() // myFunc[1] is now an object
// myFunc.length == 2

This assigns a value to myFunc[x] for a particular x (in the previous example, x = myFunc.length). At the same time, it extends the length of the array.

Although all elements of the array are properties of the array, not all properties of the array are elements of the array. You can add properties to an array that will not show up in the array's element list. For instance, you can add a method to any array much like you would add a method to other objects:

myArray.myMethod = myFunc

myArray will have the myMethod property, but it will not be between myArray[0] and myArray[myArray.length-1] unless you specifically add it as a member of the element list.

You cannot refer directly to an element's index number:

alert(myArray.0) // throws exception

This can get a bit confusing when you see an error message like this in IE: myArray.0.constructor is not an object. A numbered property like this is always an array's index number. Netscape's error message is clearer: myArray[0] has no properties.

An Array's Elements Can Be Any Object or Value

A new array begins only with the elements explicitly defined in the constructor or literal notation. You can assign to every element of the array whatever object or literal you wish. This includes objects, numbers, strings...whatever you want.

A common practice among JavaScripters is to create two-dimensional arrays by creating arrays as elements of a parent array:

myArray = new Array()
myArray[0] = new Array()
myArray[0][0] = new Object()
myArray[0][1] = new Object()
// ...

I have come up with a number of uses for arrays. One of them is the emulation of java.math.BigDecimal() objects, mentioned briefly in the preceding chapter. Each BigDecimal() object as I implement has an array of four-digit numbers, with an optional minus sign and a mandatory decimal point somewhere in the array's element list, which I designate as properties. By attaching various methods for comparison and arithmetic, I make it possible for JavaScript to handle numbers with any length of digits. (The script and its mathematics are beyond the scope of this book.)

Another use, featured as the example at the end of this chapter, is an extensible version of the switch statement, which is covered in Chapter 11, "The Global Object and Statements." By "extensible," I mean a developer can add cases to the switch at will. A similar concept allows developers to implement an emulation of method overloading, an advanced concept I covered in a limited fashion as the example in Chapter 2, "Function()." Still another use of arrays is my Basket of Windows script, which I feature as the example in Chapter 15, "window."

Populating an Array Automatically

You can define a few elements in the array (populate the array) as you define the array. The first way is to include the elements as arguments in the Array() constructor:

var myArray = new Array("red", "green", "blue")

This makes myArray[0] equal to "red", myArray[1] equal to "blue", and myArray[2] equal to "green". Another way to populate an array is to use the array literal:

var myArray = ["red", "green", "blue"]

The exception to this rule of populating an array is when you use the Array() function and give it a nonnegative integer as its only argument:

var myArray = new Array(3)

This will instead create an array with a length property of 3. On the other hand,

var myArray = new Array("3")

will create an array with a length property of 1, and myArray[0] equal to "3".

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.