JavaScript
  Home arrow JavaScript arrow JavaScript Arrays
IBM Developerworks
CIO Insight
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Download TestComplete 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

JavaScript Arrays
By: Dan Wellman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 80
    2005-05-25

    Table of Contents:
  • JavaScript Arrays
  • Multidimensional Arrays and Use
  • Beginning Code
  • Associating Images and Links
  • What to Put into the HTML

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Speeding Shipping, Improving Compliance – Read the ScanCode Systems Case Study

    JavaScript Arrays
    (Page 1 of 5 )

    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.
    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.

    More JavaScript Articles
    More By Dan Wellman


       · ...you may want to rename mySum to myProduct, since that's the value being...
       · Iteration over indexed and associative arrays would've been a big help.
       · Arrays are zero-indexed. myArray[1] is 89, not 15.Just like every other language...
       · Is not a multidimensional array, but an array of records, or structs (in c#).For...
       · document.write(myArray[1])would write 89 not 15Arrays in JavaScript are zero...
     

    JAVASCRIPT ARTICLES

    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...
    - Bulleted Menu of Links
    - Creating Click Loggers and Basic Markers wit...
    - Adding Pan Controls to Yahoo! Maps
    - Adding Zoom Controls to Yahoo! Maps
    - Working with Yahoo! Maps
    - Building Image Zooming Controls with the DOM...
    - Working with Multiple Graphics for a Zoom Ap...
    - Improving an Image Zooming Application with ...
    - Zooming in on Images with JavaScript
    - JavaScript Date Objects: Universal Coordinat...
    - Javascript Objects: More Date Methods
    - JavaScript Objects: Dates
    - JavaScript Objects: Finishing Strings

    Iron Speed

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway