
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
248
|
Chapter 11: Arrays
Array Element Indexing
An element’s position in the array is known as its index. Just as we can access the
seventh character in a string, we can access the seventh element of an array via its
index (in this case, the index is 6). We use an element’s index to set or retrieve the
element’s value or to work with the element in various other ways. Some of the
array-handling functions, for example, use element indexes to specify ranges of ele-
ments for processing.
We can also insert and delete elements from the beginning, end, or even middle of an
array. An array can have gaps (that is, some elements can be absent). We can have
elements at positions 0 and 4, without requiring anything in positions 1, 2, and 3.
Arrays with gaps are called sparse arrays.
Array Size
At any point during its life span, an array contains a specific number of elements.
The number of elements an array can hold is called the array’s length, which we’ll
discuss later in this chapter.
Creating Arrays
We can create a new array with a data literal (i.e., by simply specifying its elements
in a comma-separated list enclosed in brackets) or with the built-in array constructor
function, Array().
Array Literals
Usually, it’s more convenient to create arrays using array literals than to create them
with