Arrays are variables that can store multiple elements within them. Using an array prevents the need to use many variables to store elements. For example, take a look at these three distinct variables:
var genre1 = “Mystery” ; var genre2 = “Comedy” ; var genre3 = “Horror” ;
Wouldn’t it be easier if you could store these three separate values in a single container? You can, with the help of an array. The syntax to declare an array is as follows:
var name_of_array = [element 1, element 2…] ;
An array to store the three different variable values shown above can be declared as follows:
var genre = [“Mystery”, “Comedy”, “Horror”] ;
You can see why arrays can be very useful. Each element in an array will be assigned a unique index number. This index number is used to store and retrieve that particular element. The first element of the array is assigned the index number of 0. In the above example, the first element “Mystery” has the index number 0 while the third element “Horror” has the index number of 2. Because the array has three elements in it, the length of the array is 3.
You can also create arrays using an array constructor, the syntax for which is as follows:
var name_of_array = new Array ();
This will create an empty array with a name of your choice. It will be a blank array. You can add elements to it with the following code:
var genre = new Array (); genre = “Mystery”; genre  = “Comedy”; genre  = “Horror;
The Length Property of Arrays
The length property of an array defines the maximum number of elements that could be present in an array. The number of elements in an array cannot exceed the length of an array, though the number of elements can be less than the length of the array. For example, an array of length 7 can have 3 elements in it. It can hold 5 more elements. But doesn’t 5 + 3 make 8? Remember, the index of an array begins from 0. So, an array of length 7 can actually hold a total of 8 elements.
The length property can both get the current length of an array and set the maximum length of an array. The syntax to get the present length of an array is as follows:
The syntax to set the maximum length of an array is as follows:
length = name_of_array.length
Let’s take a few examples to show you the length property in action.
Example 1 Simple Program to Get Array Length
In the first example, let’s print the length of the array we wrote earlier:
Example 2: Using the Array Length to Get an Element
In the second example, let’s print the final element of the array we wrote earlier using the length property:
This program is a little trickier than the one we wrote before. In the first part of the program, we declare an array with three elements inside it. The length of this array is 3. In the final step of the program, we print out the value of the final element (Horror) of the array with the following line of code: “document.write(genre[genre.length-1]);”. As you can see, we have subtracted 1 from the length of the array. Why did we do that? We did that because of the fact that the index of an array always starts from the number 0. This means that the final element of a 3 length array will have an index of 2, so 1 will have to be subtracted from the length to arrive at it.
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.