J3 Limited
 
Google
WWW J3Ltd
 
JavaScript Array Object

Jumpstation:

[Contents] [Tutorial] [Object Hierrachy] [Language] [Expressions] [Statements]


An array is a programmer's term for something which can hold many objects of the same type. In the example given here the array holds the links (JavaScript's way of getting at <A ...> tags) which have been entered in this document.

One way to visualise an array in JavaScript is as a set of boxes, each with a number. The numbering starts at zero, the last box is numbered as the total number of boxes minus 1. Hence if this document has 2 links, there would be two boxes in the links array object. The length property of the array object is therefore two.

  • To access the first entry (or box) we supply the index of zero.
  • To get the second we use the number one (programmers were never great mathematicians, which is why they start counting at zero, end of digression).
  • To get at the last entry we use the notation length - 1, which in this example gives one.
  • The notation used to get at a particular entry is using the [ and ] to enclose the index we are interested in.

For example:

To access the first link object of this document we use the notation:

document.links[0]

To get at the array's only property (length), we write:

document.links.length

To get at the href property of the last link in the array,we could write the following:

document.links[document.links.length - 1].href

[Contents] [Tutorial] [Object Hierrachy] [Language] [Expressions] [Statements]


From what I can gather a JavaScript array has only one property, length, which says how many entries the array has. Consider the links array member of the document object. Here are some of its current contents:


The code to produce the above is as follows:

In the <HEAD> ... </HEAD> part a JavaScript function was defined/declared:

<SCRIPT LANGUAGE="JavaScript">
function DoPrintLinks()
{
    var nCount = 0
    var nEnd = document.links.length
    document.writeln("According to JavaScript, this document has ",

      nEnd, " link(s) in it.", "<UL>")
    for (nCount = 0 ; nCount < nEnd ; nCount ++)
    {

      document.writeln("< LI >", document.links[nCount].href,"< /LI >")
    }
    document.writeln("< /UL >")
}
</script>

Explanation:

  • The function finds out how many links are in the document and keeps hold of the value, using a variable we decided to call nEnd:
    • var nEnd = document.links.length

  • For each entry the link's href property is printed:
    • for (nCount = 0 ; nCount < nEnd ; nCount ++)
      {

        document.writeln(document.links[nCount].href)

      }


At the appropriate place in the document's body, a call to the function is placed as follows:

    <blockquote>

      <script language="JavaScript"> DoPrintLinks() </script>

    </blockquote>

Expalantion:

  • The function is called upon to print each link.

[Contents] [Tutorial] [Object Hierrachy] [Language] [Expressions] [Statements] [J3Ltd Home Page]


Note: Only the links which appear before the call to the function are printed. This is because JavaScript works its way through the document in a linear fashion. When the call to the function is encountered, JavaScript has not read the rest of the document yet.

  Copyright 2000 J3 Ltd Permission is granted to reproduce material on this page, on the condition that a reference to "WWW.J3Ltd.com" is given as the source of the material.