In this article we will take a closer look at what arrays actually are, and how to create and use them.
Now let’s move on.
Most simple way to define an array is to say that an array represents a list, or a set of items. It is an ordered list, as each array’s element has his position within the list, called index.
This index is important because we use it to target specific elements of an array. It is important to note that the first item of an array has an index of 0. The second has an index of 1, etc.
There are two ways to create an array:
- Using Array constructor
- Using literal notation [ ]
In order to create an array using Array Constructor, first you need to declare it as a variable.
Arrays differ from variables because of its ability to store multiple values, while a variable can hold only one value. If we use a constructor to create arrays, we must also use the keyword new:
As seen in the example above, we can store values of the same, but also, if we need to, we can store different data types within an array.
Using literal notation as a way of creating arrays is a preferable way to do it, and it is the method most developers use, due to its simplicity.
We just put elements, separated by commas, within the square brackets:
Now, why would we use a constructor, considering that using just square brackets is a much simpler and easier approach?
Well, array length is dynamical, meaning there is no need to specify it. However, if we need to determine the length of an array, using a constructor is preferable method. Let’s take a closer look at the example bellow.
As seen above, if we use literal notation, it will create an array of one element, with the value 10. However, using the constructor creates an array with length of 10 elements, and all of them are empty.
The purpose of this approach is to determine how many elements will be stored in our array, but the elements and values will be added later.
If needed, we can create a completely empty array, without a determined number of elements. No matter which method we use to create it, leaving empty brackets create an empty array.
How to access elements in already declared Array?
If we need to use some element from an array, we use the already mentioned index.
Each index represents the position of an element, starting from 0.
For accessing specific element in an array, we use the syntax arrayName[index]:
We can use this property to change any item we need:
It is possible to add a new element on the end of an array. Next available index in our ‘fruits’ array is 4 :
It is not necessary to add an element on the next available position (index). We can add new element on any later index, and all elements between will be empty elements:
We have two ways to declare an array: using array constructor and literal notation. Which one you will use, depends on which way suits better to a certain need, or sometimes of your code writing habit. In essence, the end result is in most cases the same, no matter which method you use.