Home arrow JavaScript arrow Page 2 - An in depth discussion of JavaScript Arrays
JAVASCRIPT

An in depth discussion of JavaScript Arrays


This series of articles mainly concentrates on working with JavaScript arrays. We shall start with the basics of JavaScript arrays and finally conclude with complex object based arrays in JavaScript. You can reuse these scripts to inject into server side controls easily (especially in .NET and Java).

Author Info:
By: Jagadish Chaterjee
Rating: 4 stars4 stars4 stars4 stars4 stars / 14
February 28, 2006
TABLE OF CONTENTS:
  1. · An in depth discussion of JavaScript Arrays
  2. · Working with single dimensional arrays: discussion
  3. · Working with two dimensional arrays
  4. · Working with two dimensional arrays: discussion
  5. · Another way to work with two dimensional arrays

print this article
SEARCH DEVARTICLES

An in depth discussion of JavaScript Arrays - Working with single dimensional arrays: discussion
(Page 2 of 5 )

Within the code in the previous section, I mainly created a simple button (which is identified as “Button1”).  The button is defined with an “onclick” event which calls a JavaScript function, “Button1_onclick”, which is defined as follows:

function Button1_onclick() {
Show();
} 

The above function simply calls another JavaScript function named “Show.”  The function “Show” is defined as follows:

function Show()
{
      var myArray = new Array();
      myArray[0] = "Jag";
      myArray[1] = "Chat";
      myArray[2] = "Win";
      myArray[3] = "Dhan";
      for (var i = 0; i < myArray.length; i++) 
      {
            document.write(myArray[i] + "<BR>");
      }
}

Let us discuss the above function part by part. 

      var myArray = new Array();

The above statement creates an array named “myArray.”  “Array()” is a built-in definition available in the JavaScript language.  You can create as many named arrays as possible (within a single web page).

      myArray[0] = "Jag";
      myArray[1] = "Chat";
      myArray[2] = "Win";
      myArray[3] = "Dhan";

To place values (or data) in an array, we need to specify the position (or index) within the array. The first statement in the above fragment simply places a value “Jag” (of type string) into the first position or index (identified by 0).  Similarly, “chat” is placed in second position (identified by 1) and so on.  Now, let us go through the last part:

      for (var i = 0; i < myArray.length; i++) 
      {
            document.write(myArray[i] + "<BR>");
      }

To display all the values in an array, we need to know the number of values available (or occupied) in an array.  This can be achieved by using the “length” property of any array object.  Within the above loop, we go through every position (or index) in an array starting from the first location (or 0th index) and proceeding through the last (using the “length” property). 

Finally we write back to the browser using “document.write”.  Within  “document.write” we specify the array name along with the index (so that the value at that index gets retrieved), concatenated together with the “<BR>” tag for line separation.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




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