The last chapter documented the JavaScript object type--a data structure that contain named pieces of data. This chapter documents the array type--a data structure that contains numbered pieces of data. Note that the arrays we'll be discussing in this chapter are not the same thing as the "associative arrays" described the previous chapter, although, as we'll see, there is not as much difference among associative arrays, the "regular" arrays described here, and objects as it might first appear.
An array is a data type that contains or stores numbered pieces of data. Each numbered datum is called an element of the array, and the number assigned to an element is called its index. Because JavaScript is an untyped language, an element of an array may be of any type, and different elements of the same array may be of different types. Array elements may even contain other arrays, which allows you to create data structures that are arrays of arrays.
You access an element of an array using the [] operator. A reference to the array should appear to the left of the brackets. Inside the brackets should appear an arbitrary expression that has a non-negative integer value. You can use this syntax to both read and write the value of an element of an array. Thus, the following are all legal JavaScript:
value = a[0]; a[1] = 3.14; i = 2; a[i] = 3; a[i + 1] = "hello"; a[a[i]] = a[0];
In some languages, the first element of an array is at index 1. In JavaScript, as well as C, C++, and Java, however, the first element of an array is at index 0.[1]
[1] Although, as we'll see later, index 0 is often not used in the Navigator 2.0 version of JavaScript.
In languages like C and Java, arrays have a fixed number of elements that must be specified when you create the array. This is not the case in JavaScript--arrays can have any number of elements, and you can change the number of elements at any time.
To add a new element to an array, simply assign a value to it:
a[10] = 10;
Arrays in JavaScript are sparse. This means that array indexes need not fall into a contiguous range of numbers, and that memory is allocated only for those array elements that are actually stored in the array. Thus, when you execute the following lines of code, JavaScript allocates memory only for array indexes 0 and 10,000, not for the 9,999 indexes between.
a[0] = 1; a[10000] = "this is element 10,000";
Once an element of an array has been defined, you can set its value to null or anything else, but there is no way to actually undefine that element, short of actually truncating the array, which (as we'll see later in the chapter) is possible in Navigator 3.0.
The Select.options[] array is an exception to this rule. It represents HTML elements in client-side JavaScript and has special behavior in Navigator 3.0, including the ability to delete individual elements. See the entry in the reference section of this book for more.
file: /Techref/language/JAVA/script/definitive/ch08_01.htm, 7KB, , updated: 2019/10/14 15:00, local time: 2024/11/19 18:31,
3.129.45.187:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://massmind.org/techref/language/JAVA/script/definitive/ch08_01.htm"> [Chapter 8] Arrays</A> |
Did you find what you needed? |
Welcome to massmind.org! |
Welcome to massmind.org! |
.