Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

jobs.webdeveloper.com

e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Creating and Naming Arrays

As with variables, naming your array is something of a test in creativity. Name it too long and you'll have to type more than you probably want. Name it too short, and you might not understand what the thing does at a later date. Some advice: use two words, keeping each word about five or six letters long. Capitalize the first letter of the second word to make for easier reading, but keep in mind that JavaScript watches the case of a letter, so the name will have to be repeated letter for letter, exactly. Obviously, you would choose something descriptive which is applicable to the implementation you are attempting.

To create a new array, follow one of the following simple syntax examples, of which there are three variations.

  1. var arrayName = new Array() - This is probably the most commonly used way of creating a new array. It initialy holds no values, nor does it have any length associated with it. It merely exists, ready to be worked with. It is empty because nothing is given within the opening and closing brackets, called the parameters of the array.
  2. var arrayName = new Array(array size) - This method is used when you know the length of the array you'll be working with, but don't have the data to stuff into it at the time of creation. The array size parameter is a whole number, an integer. You cannot have a fraction of an array exist. This array size parameter is fully accessible to the length property that exists for all array objects. Once you specify the length of an array, you may have empty items within the array, but you can't stuff more items than the array has space for into the array. You must redefine the array using one of the many methods available to the array object. More on that later. It should be noted that JavaScript version 1.2 doesn't really work with this method. The number stated as the length of the array is assumed to be the data item you want to stuff into position one of the array. Use this method when explicitly stating within the HTML SCRIPT tag a LANGUAGE attribute of JavaScript1.1 only.
  3. var arrayName = new Array(data1, data2, data3, etc) - This method of creating an array both defines the length of the array and the data to be held as the items of the array. As stated earlier, the array can be of any size you have a need for. The data1, data2, and data3, parameters are the data that is to be stuffed into the array. The items will be indexed on a first come, first served basis. That is, data1 will be in position 1, data2 will be in position 2, and data3 will be in position 3. This follows in the same manor for every item you stuff into the array using this method.
  4. var arrayName = [data1, data2, data3, etc] - This method of creating an array works exactly the same as the syntax example number three, with a few differences. Note that the "new Array" keywords are left out. JavaScript sees this as entirely legal -if square brackets are used to hold the data items instead of brackets.

Notice that for every method of creating an array the "var" keyword is used. This is essential to the creation of an array. There are no exceptions. The array must use the var keyword because it is essentially a higher form of variable. The new keyword must be used because you're creating a new instance of the core JavaScript Array object, with one exception shown in the syntax example, number 4. You may state as many different arrays as you have a need for. You are not limited to one array.

Now that you have a glimmer of how an array is created, we'll build your knowledge by including a working example of how to access the data items within your array. Its really quite easy to do. Examine the example.

var arrayOne = new Array("This ", "is ");
var arrayTwo = new Array("a ", "string ");
var arrayThree = new Array("of " , "text");

document.write(arrayOne[0] + arrayOne[1] + arrayTwo[0] +
 arrayTwo[1] + arrayThree[0] + arrayThree[1]); 
The example begins with the creation of three arrays called arrayOne, arrayTwo, and arrayThree. They are stuffed with data of the String type. This is evident because the data is enclosed within quotes. The document.write statement (which you'll learn to use as we go) then writes the contents of the arrays to the screen. Notice that each array was created with two items per array. Think back to how the numbering system works within an array. Everything starts at zero and counts up from there. From looking at the example, you can figure out that the contents of an array are accessed by stating the name of the array (exactly - case matters, remember) along with its index number. The index number is enclosed within square brackets. Simple. We'll have more examples for you to examine as we go further into working with arrays. The output of the document.write statement looks as follows.

This is a string of text 

I should note that when you reference a position within an array that doesn't exist, the JavaScript engine will return a value of undefined.

The JavaScript Array
JavaScript Introduction
String Referenced Array Indexes

The JavaScript Chronicles

JavaScript Introduction
Part 2: Data Types
Part 3: Arrays
Part 4: Operators
Part 5: Conditional Statements
Part 6: JavaScript Functions
Part 7: Pattern Matching - The RegExp Object
Part 8: Introduction to Server Side JavaScript
Part 9: Server Side JavaScript Mail Sending
Part 10: Server Side JavaScript and File Manipulation
Part 11: Working with Forms in JavaScript Part 12: Getting to Know Dynamic HTML


Up to => Home / Authoring / Tutorials




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers