SHARE
Facebook X Pinterest WhatsApp

The JavaScript Diaries: Part 13

Written By
thumbnail
Lee Underwood
Lee Underwood
Apr 23, 2007

Now that we know about the different types of arrays, we’ll learn how to manipulate them in order to make them more functional. In this section, we’ll look at the properties and methods that are commonly used for most coding situations.

Array Properties

length

The length property returns the number of elements in an array. The format is arrayName.length. The length property is particularly useful when using a loop to cycle through an array. One example would be an array used to cycle banners:

var bannerImg = new Array();
  bannerImg[0]="image-1.gif";
  bannerImg[1]="image-2.gif";
  bannerImg[2]="image-3.gif";

var newBanner = 0
var totalBan = bannerImg.length

function cycleBan() {
  newBanner++
  if (newBanner == totalBan) {
    newBanner = 0
  }
  document.banner.src=bannerImg[newBanner]
  setTimeout("cycleBan()", 3*1000)
}
window.onload=cycleBan;

This portion is then placed in the body where the banner is to be displayed:

<img src="image-1.gif" name="banner">

Let’s take a look and see what happened here:

  1. On the first line, we created a new instance of the array bannerImg, and gave it three data elements. (Remember, we are only making a copy of the Array object here.)
  2. Next, we created two variables: newBanner, which has a beginning value of zero; and totalBan, which returns the length of the array (the total number of elements contained in the array).
  3. Then we created a function named cycleBan. This function will be used to create a loop to cycle the images.
    1. We set the newBanner variable to be increased each time the function cycles. (Review: By placing the increment operator [” ++ “] after the variable [the “operand”], the variable is incremented only after it returns its current value to the script. For example, its beginning value is “0”, so in the first cycle it will return a value of “0” to the script and then its value will be increased by “1”.)
    2. When the value of the newBanner variable is equal to the variable totalBan (which is the length of the array), it is then reset to “0”. This allows the images to start the cycle again, from the beginning.
    3. The next statement uses the Document Object Method (DOM – we’ll be taking a look at that soon) to display the images on the Web page. Remember, we use the dot operator to access the properties of an object. We also read the statement backwards, i.e., “take the element from the array bannerImg, that is specified by the current value of the variable newBanner, and place it in the src attribute located in the element with the name attribute of banner, which is located in the document object.”
    4. We then used the setTimeout function to tell the script how long to display each image. This is always measured in milliseconds so, in this case, the function cycleBan is called every 3,000 milliseconds (i.e., every 3 seconds).
  4. Finally, we used the window.onload statement to execute the function cycleBan as soon as the document is loaded.

There are a total of five properties for the Array object. In addition to the length property listed above, the others are:

  1. constructor: Specifies the function that creates an object’s prototype.
  2. index: Only applies to JavaScript arrays created by a regular expression match.
  3. input: Only applies to JavaScript arrays created by a regular expression match.
  4. prototype: Used to add properties or methods.

The other properties listed here are either more advanced or seldom used. For now, we’ll stick to the basics.

Recommended for you...

The Revolutionary ES6 Rest and Spread Operators
Rob Gravelle
Aug 23, 2022
Ahead of Time (AOT) Compilation in Angular
Tariq Siddiqui
Aug 16, 2022
Converting a JavaScript Object to a String
Rob Gravelle
Aug 14, 2022
Understanding Primitive Type Coercion in JavaScript
Rob Gravelle
Jul 28, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.