JavaScript: Arrays
Some of the time when creating scripts, we want to store lots of values which are related in some way. For example if you wanted to store the grades that students got in an exam, you'd want various integer values of what scores different students got. We can store lots of related data like in the example, by using these things called arrays.
Arrays are basically just special types of variables which can hold multiple values. They are often used for lists of data, as in our example of different student's scores. We can declare arrays in JavaScript by starting like a regular var
declaration, and then setting this variable to a new array. We can create this "new array" that we have to set it to, by using the new
keyword, and then writing Array
. We then provide "Array" with empty brackets (no parameters - for now) and end the line with a semicolon:
1
|
|
So the above would create an empty array called 'scores'. Now we probably want to populate it with some values. We reference different "elements" of, or different pieces of data in, an array by specifying the index number (kind of like an ID number to identify different values in the array) in square brackets. The first element (value) in an array has an index number of 0 (computers like counting from 0), the second has an index of 1, the third has an index of 2, and so on. So if we want to set the first element (the one with the index of 0) in our 'scores' array to 20, we might use something like this:
1
|
|
We can set other elements in the array using the same method:
1 2 3 4 5 |
|
We can also change values in an array this way - so if scores[0]
was already set before we executed the code above, it would now be 20. Incidentally - using square brackets with the index value is also the way we can access the values from our array! So if we wanted to output the first element (index 0) of our array in an alert box, we could do the following:
1
|
|
If we want an array to only be able to hold a certain number of values, we can specify a number in the parameters when we create the array. For example:
1
|
|
It's also worth noting that we can initialise the array when we declare it by passing Array
multiple parameters (and it will automatically assign these to the array). For example, if we want to pass in some values for the first five elements of our array (index 0 to 4) we could do:
1
|
|
We can now see that the values have set by accessing the values and alerting them out:
1 2 3 4 5 |
|
It's worth noting that all the code above could easily be implemented into a loop to make it much cleaner, more dynamic, and more efficient. The easiest way is probably to use a 'for' loop (as covered in the previous tutorial). I suggest having a go at doing this yourself, but if you just want quick answers, the JavaScript for this is in a JSFiddle below:
As a side-note to finish off the tutorial - arrays can store any datatype you want, whether it be strings (one or more characters 'stringed' together), doubles (numbers with decimal places) or simply integers (whole numbers - as we demonstrated in this tutorial). This creates a very flexible and strong way to store a number of related pieces of data. If you're feeling up to the challenge, try using multiple arrays to list the prices and names of items in a shop.