Array in JavaScript is a global object with many methods and properties to perform different operations. Using arrays, often one has to deal with its length. In JavaScript there are several ways to access array size.

In general the length of an array is not a fixed value, it may change or have null values as well. All in all, the length property is used to retrieve the number of items held in a particular array.

Array.size()

We should begin by saying that the size() method is not fully valid. It is not a native array method. If you really want to use the size() method, you need to connect additional libraries. For example size() is available in JQuery and other libraries, not in JavaScript. However, even in a library, the use of size() is considered irrelevant, so it is better to use the length method.

Size() is the method, which returns the length property. So there is no reason to use the method which returns property if you can call property directly.

Array.length

Length is relevant for JavaScript property, it can set the number of elements in an array or return this value. Array length property length is used both independently and in connection with some numerical properties. Some array methods (slice, join, indexOf, etc.) use the length property value in their execution. Another group of methods (push and splice), as a result of their work, update the length property of the array.

The length property is automatically updated when you add new elements to an array or delete.

Syntax

In case you want to set size of array:

  • the array is created using a literal and the value of the length is assigned explicitly:

var arrayName = [];
arrayName.length = value;
  • the array is created using a constructor, one numeric value is passed to the constructor as an argument. An empty array will be created, the value of the length property of this array is equal to the number passed to the constructor:

var arrayName = new Array(value);
alert(arrayName.length);    // value

In case you want to get size of array:

arrayName.length

Return value. The method always returns a numeric value, which will be the size of the array. As a rule, it is a 32-bit integer value. By the way, the returned value is always numerically greater than the highest index in the array. This is due to the fact that the index value starts with 0.

Examples

We will show you examples of how to get the length of an array, how a property can change the length of an array, and what other ways you can use the length of an array.

In this example, we declare an array without initializing it with values and set the size of the array. Аs a result of the size of the array we get the value we set.

var ages = new Array();
ages.length = 5;	
alert(ages.length);		// 5 as a result

This time we add some values to our ‘ages' array and ask the program how many values were added:

var ages = [25, 22, 20, 26, 23];
alert(ages.length); 	// 5 as a result

Such an array is called a dense array - an array where elements have contiguous indexes starting at zero.

In the next case our array is sparse, that is, some values are omitted. The program will return a number which is greater than the highest index in the current array. Because the length doesn't indicate the actual number of elements.

var ages = [25, , 20, 26, 23];
alert(ages.length);		// 5 as a result

Although in this example the undefined values are at the end of the array, the program still returns the value greater than the highest index:

var ages = [25, 22, 20, 26, 23, , , , , , ];
alert(ages.length);     // 10

Also you can set the length property of an array to a value that is higher than the highest index. Such an array will be named "spare".

In this example we declare an array of 5 values. Then add a value with index 20, thus forming a sparse array. Items with indexes 6 through 19 will not be specified. And as a result we get a number 21.

var ages = [25, 22, 20, 26, 23];
ages[20] = 27;
alert(ages.length);		// 21 as a result

This also applies when all values in the array are undefined and we add an empty element by the index. As a result we will receive a number which is greater than the value declared by us.

In addition, increasing the value of the property length, new elements will not be added to the array, only the value of the property will be changed.

var ages = new Array;
ages[25] = ' ';
alert(ages.length);	    // 26

Setting the index we can change the appearance of the array. In particular if you set the length property to a value that is lower than the highest index in this array, all the elements whose index is greater than or equal to the new length are removed:

var ages = [25, 22, 20, 26, 23];
ages.length = 2;
alert(ages);    //25, 22

So the simplest way to clear an array is:

ages.length = 0;

When initializing an array we can specify data type instead of numeric indexes:

var ages = new Array();
ages['0'] = 25;
ages['1'] = 22;
ages['2'] = 20;
alert(ages.length); 	// 3

In this case, we wrote the numbers as signs, and it resulted in the number 3. However, if we were to specify it as indexes of letters, words or signs, the program would not be able to recognize them:

var ages = new Array();
ages['a'] = 25;
ages['b'] = 22;
ages['c'] = 20;
alert(ages.length); 	// 0

Most often the length property is used to work with an array in a loop. In this example, let's just output all the elements of the array in a loop.

var ages = [25, 22, 20, 26, 23];
for (var i = 0; i < ages.length; i++)
{
    alert(ages[i]);
}

The length of the array can be used for other purposes. For example, to find the arithmetic mean.

Let's declare an array and a sumOfAges variable that will store the sum of all values from the array. In a cycle we add all age values and when values in an array will come to an end, we will divide the sum on length of an array.

var ages = [25, 22, 20, 26, 23];
var sumOfAges = 0;
for (var i = 0; i < ages.length; i++)
{
    sumOfAges += ages[i];
}
var avarageAge = sumOfAges / ages.length;
 
alert(avarageAge); 		// 23,2

Finally, a few more words about sparse arrays. We have already found that the property is not the actual number of elements in the array, but the largest digital index plus one.

That is, if we create an array with the length 100, but give a value to only two of them and the other will be undefined, the property will still return value 100. To determine the actual number of items you can use this code:

var arr = new Array(100);
arr[25] = 1;
arr[80] = 4;
var actualNumberOfElem = 0;
for(var i = 0; i < arr.length; i++)
{
    if (arr[i] != undefined)
    {
        actualNumberOfElem++;
    }
}
alert(actualNumberOfElem);      // 2, but not 100