Introduction
Overview
History
Advice
Books
Tutorials
Source Code
Top 100
Reference

Beginners
Sample Code
IDE
Syntax
Variables
Boolean Logic
Strings
Arrays
Conditional Flow

Intermediate
Date-Time
Math
Functions
Event Handlers
Error Handling

Advanced
Objects
Dynamic HTML
Cookies
Multimedia
CGI
Java Applets
Distribution

Community
Web Sites
Mailing Lists
USENET
Vendors
News

GBIC >> JavaScript >> Arrays
Arrays are a common programming language concept and are simply a means of holding multiple values within a single variable. Each element of an array can hold a value of any type (number, string, boolean, object).

Create an Array
As with any variable, array variables can be used without prior declaration. To create an array without prior declaration, just assign an element a value, as in the following example:

X[5] = 25

The index, or position, of the element within the array is placed within the square brackets ([]). In this example, the 6th element of the array (arrays are zero based) is assigned a value of 25. This statement both creates an array with 6 elements and assigns the 6th element a value of 25. All other elements will have a value of 'undefined' until a value is assigned to them by other JavaScript statements.

It is considered better programming practice to declare arrays before referencing them in code. Arrays can be declared in two ways, as shown in this next example:

var X = new Array()                //array of undefined length
var X = new Array(5)               //array of length 6
var X = new Array(1,2,"a",4,"b")   //array of length 5 & initializes values

Note in the last example, that each element of the array can be any data type.

Array Indexes In the prior examples, array elements were referenced using numbers but JavaScript arrays can also be indexed with a string. For example, an element of array X would be assigned as in the following example:

X["one"] = 25

Once the array element is assigned using a string as an index, the element of the array can be reference in two ways, as in the following example:

Y = X["one"]
or
Y = X.one     //where 'one' is the string index
              // of the element being referenced

Multi-dimensioned Arrays
Multidimensioned arrays are not directly supported in JavaScript. A workaround is to assign an array as the element of an array, as in the following example:

part1 = new Array(1,2,3,4)
part2 = new Array(5,part1)

Then, access elements in the multidimensional array using a double bracket opertator: part2[1][2]

Array Object Properties and Methods

  • Propertes:
    • length: number of elements in the array
    • prototype
  • Methods
    • concat(): concatenates an array on to an array
    • reverse(): reverse order of elements in an array
    • slice(): returns a subsection of an array
    • sort(): sorts elements in an array
    • splice(): inserts or removes elements from an array
    • push(): add element to end of an array
    • pop(): delete last element from an array
    • unshift(): add elements to front of an array
    • shift(): remove elements from front of an array
    • join(): concatenates all elements into one string
    • toString(): converts elements to a string
    • toSource(): converts elements to a string with square brackets