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


The JavaScript 1.1 Array

I'm including the JavaScript 1.1 methods for working with arrays here for those older applications that will some day require your attention. They work a bit different because in that version of JavaScript, there wasn't a self-replicating array object like the kind available to more recent version (starting with JavaScript 1.2). To create the equivalent to an array with this version of the language, you need to first create and instance of the Object() object and name it. You would name it something descriptive, of course. Once created, you may assign to the Object object the properties you require using the [ ] operators to stuff values into the object. You must keep track of the length of the array on your own - this version of JavaScript doesn't recognize the length property of the more recent versions. By assigning a length property to the object and assigning a value to it, you may be able to return the length of the array. The downside to this workaround is that the newly created length property will be an item of the array and occupy the number one position. Since the Zero Based Index method is used, length will be in position [0]. Examine the example below.

var arrayStuff = Object();
arrayStuff.length = 3
arrayStuff [1] = " red";
arrayStuff [2] = " green";
arrayStuff [3] = " blue";
document.write("The contents of the array are : " +
 arrayStuff[1] + arrayStuff[2] + arrayStuff[3]) 

The example shows first the creation of a new Object() object. The first position of the hypothetical array is taken by the length property, which is set to 3. The rest of the array is created with the values of red, green, and blue - which are strings because they're within quotes. Referencing of the array contents start at position [1] instead of position [0] because the length property occupies position [0]. You would reference the array in the same manner as in JavaScript 1.2, as shown within the document.write statement. Simple. The output of the above example is shown below.

The contents of the array are : red green blue 

Now since we included a length property in the example, we might as well use it. Examine the following variation of the above example which utilizes the length property within a for statement (which you'll learn more about eventually - be patient).

var arrayStuff = Object();
arrayStuff.length = 3
arrayStuff [1] = " red";
arrayStuff [2] = " green";
arrayStuff [3] = " blue";

document.write("The contents of the array are : ");

for (n=1; n<=arrayStuff.length; n++) {
   document.write(arrayStuff[n], " ");
   } 
The example shows the use of the length property of the arrayStuff object being used within a for statement. The conditional statement as the condition of the for statement, which is within the brackets, takes the value of the length property and increases it by one. This is your first instance of seeing a loop function. The initial value is 1 because the length property is in position 0 within the array - arrays are zero based indexes. The number of the index is represented by the letter n which initially has a value of one as defined by the "n=1" statement. This value of n is then referred to the arrayStuff.length property with the "n<=arrayStuff.length" statement. Finally, the value of n is increased by one with the "n++" statement using the "increase by one" JavaScript operator. Within the opening and closing braces of the for statement is a document.write statement which calls upon the condition within the brackets of the for statement. The position within the array is represented by the letter n within the square brackets, telling the browser to interpret the condition within brackets until it runs out of items in the arrayStuff array. The results are written to the screen and should look as follows.

The contents of the array are: red green blue 
That's it for arrays. You now have the knowledge required to advance on to the more in depth JavaScript statements such as conditional expressions and loops. Variables and arrays are used extensively in these operations. Next week The JavaScript Chronicles covers Operators.

The Length Property
JavaScript Introduction
JavaScript Operators

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