Accessing Array Elements with Indexes

This section provides a tutorial example on how to access array elements with element indexes.

Array elements can be access with array indexes with several simple rules:

Here is a tutorial example JavaScript that shows you how to store or retrieve array elements with element indexes:

<!-- Access_Array_Elements.html
   Copyright (c) 2002 All Rights Reserved.
<head><title>Access Array Elements</title></head>
<script type="text/javascript">

   // Creating an empty array
   var glossary = new Array();

   // Adding the first element
   glossary[0] = "JavaScript";

   // Adding the second element at index 10
   // This extends the array length to 11
   glossary[10] = "Web";

   document.write("Element at index of 0: "+glossary[0]+"\n");
   document.write("Element at index of 5: "+glossary[5]+"\n");
   document.write("Element at index of 10: "+glossary[10]+"\n");
   document.write("Element at index of 15: "+glossary[15]+"\n");

The output of this sample JavaScript is:

Element at index of 0: JavaScript
Element at index of 5: undefined
Element at index of 10: Web
Element at index of 15: undefined

Table of Contents

 About This Book

 Introduction to JavaScript

 ECMAScript Language Specification and JavaScript Dialects

 Data Types, Variables and Expressions

 Flow Control Statements

Creating, Accessing, and Manipulating Arrays

 What Is an Array

 Creating an Array Object

Accessing Array Elements with Indexes

 Truncating and Iterating Array Elements

 Array Object Instance Methods

 Array Object Instance Method Examples

 Defining and Calling Functions

 Web Browser Supporting JavaScript

 Server-Side and Client-Side Web Scripting

 Introduction to Objects

 Defining Your Own Object Types

 Inheritance of Properties and Methods through the Prototype Object Chain

 'jrunscript' - JavaScript Shell Command from JDK

 Using Functions as "Function" Objects

 Introduction to Built-in Object Types

 W3C's Document Object Model (DOM) Specifications

 AJAX (Asynchronous JavaScript And XML)


 Full Version in PDF/EPUB