Arrays

Arrays are handy if you need to store a collection of values/objects. In JavaScript, an array is represented by the Array class. For example, this code constructs a JavaScript array:

var averageTemps = new Array()

Once you have an instance of Array, you can add to it as many elements as you wish. JavaScript arrays can grow as needed. Remember though that the numbering in JavaScript arrays starts from zero:

averageTemps[0] = 10
averageTemps[1] = 12
averageTemps[2] = 8

If you know that the exact number of values you are going to store in your array is, say, 30, you can declare the array like this:

var averageTemps = new Array(30)

This will make your script slightly more efficient, but in most cases the difference will not be noticeable.

Yet another way to create an array is to just list all its members, like this:

var averageTemps = new Array(10, 12, 8, 15, 11, 10, 9)

Once an array is created, you can retrieve the values stored in its members, or you can assign them new values, like here:

averageTemps[1]
averageTemps[2] = 14

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset