Home arrow JavaScript arrow JavaScript Arrays
JAVASCRIPT

JavaScript Arrays


Arrays in JavaScript are extremely useful for storing and manipulating information you have coded directly into the script, or information collected from the browser. In this article, Dan Wellman details how to use and manipulate them.

Author Info:
By: Dan Wellman
Rating: 3 stars3 stars3 stars3 stars3 stars / 110
May 25, 2005
TABLE OF CONTENTS:
  1. · JavaScript Arrays
  2. · Multidimensional Arrays and Use
  3. · Beginning Code
  4. · Associating Images and Links
  5. · What to Put into the HTML

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
JavaScript Arrays
(Page 1 of 5 )


This article has three files to download, available here: Script & HTML, Table Images, Other Images.

The array is one of a number of objects built directly into JavaScript. Think of them simply as variables containing multiple values. They can hold string or numerical values, have no maximum boundaries (although the speed at which your scripts are interpreted will decrease as the amount of data increases) and are comma delimited. They have no persistence, meaning that their values are not held once the page containing the script has closed or reloaded.

The aim of this article is to show you how information can be hard-coded into your scripts and how that information can be addressed and used to enhance your Web pages. The example script will focus just on one dimensional arrays.

Single Dimensioned Arrays

A simple example of a single-dimensioned array is:

myArray = new Array(15,89,4,61,5)

Which simply says create an array called myArray with five values, the values are integers and equal 15, 89, 4, 61 and 5. This information, were it to mean anything, could then be manipulated in all kinds of ways; you could print the results on the page using the simple command:

document.write(myArray[1])

That would display 15 on the page. You could also perform simple math, were the need to arise:

var mySum = myArray[1] * myArray[2]
document.write(mySum)

which displays the number 356.

An important property of the array object is the length property; the extremely basic numerical array above has a length of five. Each of the values in the array can be accessed using their index within square brackets following the array name, for example:

myArray[2]

It’s important to remember that the indices of any array always begin with 0 representing the first item in the array; therefore, while the length property of this very simple array is five, the highest index is four.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks
- Dynamic jQuery Styling

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials