Chapter: Internet & World Wide Web HOW TO PROGRAM - The Ajax Client - JavaScript: Arrays

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

JavaScript: Arrays

An array is a group of memory locations that all have the same name and normally are of the same type (although this attribute is not required in JavaScript).

Chapter 10

JavaScript: Arrays

Introduction

 

This chapter serves as an introduction to the important topic of data structures. Arrays are data structures consisting of related data items (sometimes called collections of data items). JavaScript arrays are “dynamic” entities in that they can change size after they are created. Many of the techniques demonstrated in this chapter are used frequently in Chapters 12–13 as we introduce the collections that allow a script programmer to manip-ulate every element of an XHTML document dynamically.

 

Arrays

 

An array is a group of memory locations that all have the same name and normally are of the same type (although this attribute is not required in JavaScript). To refer to a particular location or element in the array, we specify the name of the array and the position number of the particular element in the array.

 

Figure 10.1 shows an array of integer values named c. This array contains 12 ele-ments. Any one of these elementsmay be referred to by giving the name of the array fol-lowed by the position number of the element in square brackets ([]). The first element in every array is the zeroth element. Thus, the first element of array c is referred to as c[0], the second element of array c is referred to as c[1], the seventh element of array c is referred to as c[6] and, in general, theith element of array c is referred to as c[i-1]. Array names follow the same conventions as other identifiers.

 

The position number in square brackets is called a subscript (or an index). A subscript must be an integer or an integer expression. If a program uses an expression as a subscript, then the expression is evaluated to determine the value of the subscript. For example, if we assume that variable a is equal to 5 and that variable b is equal to 6, then the statement

 

c[ a + b ] += 2;

 

adds 2 to array element c[ 11 ]. Note that a subscripted array name is a left-hand-side ex-pression—it can be used on the left side of an assignment to place a new value into an array


element. It can also be used on the right side of an assignment to assign its value to another left-hand side expression.

 

Let us examine array c in Fig. 10.1 more closely. The array’s name is c. The length of array c is 12 and can be found using by the following expression:

 

c.length

 

Every array in JavaScript knows its own length. The array’s 12 elements are referred to as c[ 0 ], c[ 1 ], c[ 2 ], …, c[ 11 ]. The value of c[ 0 ] is -45, the value of c[ 1 ] is 6, the value of c[ 2 ] is 0, the value of c[ 7 ] is 62 and the value of c[ 11 ] is 78To calculate the sum of the values contained in the first three elements of array c and store the result in variable sum, we would write

 

sum = c[ 0 ] + c[ 1 ] + c[ 2 ];

 

To divide the value of the seventh element of array c by 2 and assign the result to the vari-able x, we would write

 

x = c[ 6 ] / 2;

The brackets that enclose the array subscript are a JavaScript operator. Brackets have the same level of precedence as parentheses. The chart in Fig. 10.2 shows the precedence and associativity of the operators introduced so far in the text. They are shown from top to bottom in decreasing order of precedence, alongside their associativity and type.



Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.