Array length

We will learn more about how arrays work and, specifically, we will see how to use their length property to access the number of cells it has.

In the previous article we started to explain the . In this article we are going to continue the theme, showing the use of its length property.

All javascript arrays, apart from storing the value of each of their cells, also store the number of positions they have. To do this they use a property of the array object, the length property. We will see what a property is in objects, but for our case we can imagine that it is like a variable, in addition to the positions, that stores a number equal to the number of cells that the array has.

To access a property of an object, use the dot operator. Write the name of the array that we want to access to the number of positions it has, without brackets or parentheses, followed by a point and the word length.

var myArray = new Array() myArray = 155 myArray = 499 myArray = 65 document.write(“Array length: ” + myArray.length)

This code would print on the screen the number of positions in the array, which in this case is 3. Remember that an array with 3 positions goes from position 0 to 2.

It is very common to use the length property to be able to go through an array through all its positions. To illustrate it, we are going to see an example of traversing this array to show its values.

for (i=0;i

Note that the for loop is executed whenever i is less than the length of the array, taken from its length property.

The following example will help us to better understand the paths through the arrays, the operation of the length property and the dynamic creation of new positions. We are going to create an array with 2 positions and fill in its value. Later we will introduce a value in position 5 of the array. Finally we will print all the positions of the array to see what happens.

var myArray = new Array(2) myArray = “Colombia” myArray = “United States” myArray = “Brazil” for (i=0;i“) }

The example is simple. You can see that we do a tour of the array from 0 to the number of positions in the array (indicated by the length property). In the tour we are printing the number of the position followed by the content of the array in that position. But we may have a doubt when we ask ourselves what the number of elements of this array will be, since we had declared it with 2 and then we have introduced a third one in position 5. When we see the output of the program we will be able to answer our questions. It will be something similar to this:

Array position 0: Colombia

Array position 1: United States

Array position 2: null

Array position 3: null

Array position 4: null

Array position 5: Brazil

It can be clearly seen that the number of positions is 6, from 0 to 5. What has happened is that when entering a data in position 5, all the boxes that were not created up to the fifth are also created.

See also  Hover one element and affect another

Positions 2 through 4 are uninitialized. In this case our browser has written the word null to express this, but other browsers may use the word undefined. We will see later what this null is and where we can use it, the important thing now is that you understand how arrays work and use them correctly.

Can .

We will continue the subject of arrays in the next installment of this manual: .

Loading Facebook Comments ...
Loading Disqus Comments ...