Welcome to Bucaro TecHelp!

Bucaro TecHelp
HTTPS Encryption not required because no account numbers or
personal information is ever requested or accepted by this site

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Using the Java Script Array Object

An array is a structure for storing multiple data values. Each value in an array is given an index number. To access a particular data item in an array, use the name of the array and the index number of the particular data item.

Creating an Array

To create an Array, use new keyword. The code shown below creates an array named color that can hold three data items.

var color = new Array(3);

Putting Data into an Array

To enter data into an array, create a series of assignment statements, one for each data item in the array. The code shown below populates the color array with the names of three colors.

color[0] = "red";
color[1] = "green";
color[3] = "blue";

Note that array indexes are zero-based, in other words the index values start with 0 for the first element.

The new keyword is required only when creating an array with object type data. In the previous example we created an array of string objects. The new keyword is not required when creating an array of simple variable types. The code shown below creates and populates an array with integers.

var digit = [1, 2 ,3];

The code shown below creates and populates an array with characters.

var char = ['a', 'b' ,'c'];

Accessing Array Values

To access a particular data item in an array, use the item's index number. The code shown below displays the value of the second data item in the array of characters created above.

alert(char[1]);

Output:

b

To access the entire contents of an array, use the name of the array without an index number. The code shown below displays the value of all the data items in the array of characters.

alert(char);

Output:

a,b,c

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro



Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2024 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268