Simply stated, an array is a list. The size of the list determines how many pieces of information, known as elements, can be put into it. Any part of the list can be accessed, if you know where in the list it is stored. This is done using an index, but can also be an associative string as we will see in a later section.
There are several ways of creating an instance of an array, as follows:
The first two methods create an array that has no elements. While this may not seem useful, it is possible to add new elements to an array, which we will discuss later. The third method creates an array with a specific number of elements. These elements are initially empty, but new values can be assigned to them. The final two methods creates a populated array. The size of the array will depend on the number of elements that are declared, but in theory any number of elements can be specified. This type of constructor is very useful in cases when you want to have some type of table of predefined values that can be accessed.
The power of an array comes from the ability to access and modify the elements of the array. Elements of an array have an index number associated with them. The first element of an array is referenced with the number 0, the second is 1, and so on. The index can be thought of as an offset, with 0 being no offset, so the first element and 1 being an offset of 1 from the beginning. Some people find using element 0 confusing, so will simply ignore that element and start storing stuff in element 1. If you are going to do this, remember that the last element of an array is one less than the size of the array.
To access the element, you simply use the array variable's name, followed by the index number in square brackets. The index does not have to be a number, but can be a variable that contains the index number. For example, the following program adds the third and fourth elements (index values 2 and 3) and places the result in the first (index 0) element.
The Array class has functions for modifying and getting information about the array. It also has a read-only property variable named length that holds the current length of the array. JavaScript allows you to change the size of the array at any time. Many other programming languages are not quite as flexible. We will be taking a look at these functions in a later section.
One common thing that needs to be done is iterating over the elements in the array to do something with the element. The normal looping mechanisms can be used as the index can simply be a variable. So, using a for loop.
JavaScript has a couple of additional versions of the for statement for dealing with iterating over arrays and objects. The first of these are for iterating over the keys of an object, which will become important when we start looking at iterative arrays and classes. The format for the for..in command is for (key in object). When using this object with arrays, the key is the index of the array. Here is a sample to illustrate how this can be used.
The second method, for..of iterates over the values. This is useful if you have no need for index information but simply want the contents of the array. The format is for (value in object) with the value being the variable to hold the values while the object would be the array. Here is a sample.
Now that we are aware of arrays, in the next section we will revisit the project from the last chapter to see how the use of arrays can greatly simplify things.
A cheat sheet covering the topics in this chapter.
A breakdown of the chapter topics.
Arrays allow you to store a list of items as a single variable.
Target release date July 6th, 2025
Target release date July 13th, 2025
Target release date July 20th, 2025
Target release date July 27th, 2025
Target release date August 10th, 2025
Target release date August 17th, 2025
Target release date August 24th, 2025
Target release date September 7th, 2025
Target release date September 14th, 2025
Target release date September 21st, 2025
Target release date October 5th, 2025
Target release date October 19th, 2025