An array is an ordered collection of data, each item of data being identified by an index number. Java Script arrays can contain a variety of data types. Assigning arrays as array elements allows you to create a multidimensional array. Using simple Java Script code you could use a multidimensional array to create a fairly sophisticated database application.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site
Your Ad Here!
Your ad here for one full year, only $20. Send me your ad copy, up to six lines, I'll post it ASAP. After you inspect your ad online, you can pay through PayPal.
Click here for more information.

Java Script Arrays

An array is defined as an ordered collection of data. You can visualize an array as a list, each item in the list being identified by an index number starting with [0] for the first item.

An array can be used to store data that you allow users to search. This type of application mimics the behavior of a database program. When designing a script, consider whether handling the data as an array could have an advantage.

The browser itself creates a number of internal arrays for the html elements on your webpage. For example, it maintains an array for the links on the webpage, the first link being identified as document.links[0].

Use the new keyword to create an array. For example to create an array containing six names of colors you might use the code shown below.

var strColor = new Array(6);

Then to enter data into the array, use statements indicating which index each string should be assigned to, as shown below.

strColor[0] = "black";
strColor[1] = "blue";
strColor[2] = "green";
strColor[3] = "orange";
strColor[4] = "red";
strColor[5] = "white";

Java Script arrays have a length property. Using the declaration shown above, the strColor array is explicitly defined to have a length of 6. However, Java Script arrays are dynamic, so you could leave the length empty, as shown below.

var strColor = new Array();

This allows you to add items to the array at any time. In this case, the length property starts out with a value of 0. You can assign a value to any index in the array and the length property will automatically adjust for the new size of the array. If you know what the data will be, you can declare and initialize the array at the same time as shown below.

var strColor = new Array("black", "blue", "green", "orange", "red", "white");

• JavaScript 1.2 allows you to construct an array without the new Array keywords, as shown below.

strColor = ["black", "blue", "green", "orange", "red", "white"]

In my opinion, this is bad programming practice because, the new keyword reminds the programmer that a memory object has been created. Later in the script, when the data in the array is no longer needed, the programmer may want to release the memory using the delete keyword.

Web Design Sections

RSS Feed RSS Feed

Java Script Quick Reference
Java Script Data Types
Java Script Reserved Words
Java Script Arithmetic Operators
Comparison Operators
Java Script Arrays
Java Script Character Encoding and Decoding
The if/else Structure
The switch/case Structure
The for Loop
The while Loop
The break Statement
The continue Statement
JavaScript Math Object
Round a Number
Determine Absolute Value
Determine Minimum and Maximum
Generating Random Numbers
Java Script Trigonometric Methods
Java Script Number Object
Format a Number as Currency
Java Script Strings
Compare Two Strings
Find a Character or a Substring Within a String
Include a Quote Character in a String
Include a Backslash Character in a String
Define Lines in a String
Use Escape to Replace Dangerous Characters
Convert a Number to a String
Convert a String to a Number
The Document Object Model (DOM)
Accessing Web Page Elements
Interactively Set Webpage Colors
Get Webpage File Date and File Size
Dueling Windows
Cookie Power Made Easy
Web Designer's Reference

Your Ad Here
[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269