How to use JavaScript array length Property With Examples

The array length property

The array length property is used to return or set the length of an array. The length property is quite useful in getting the array count that you can use in the for loop to perform some desired action based on array elements.

See an example of length property

This tutorial will show you how to use the JavaScript array length property with examples but let us first look at its general syntax.

How to use the array length property?

Following is the general syntax of using array length property:

arrayname.length

To set the length of JS array:

Arrayname.length= no_of_elements

See following examples online to learn more about length property.

An array size example by length property

The following example shows how to get array size by using the length property of the array. We have created a NumArr array and the alert will show JavaScript array size by using the length property.

JavaScript array length

Experience this example online



JavaScript array length example with for loop

As mentioned earlier, you can use array length property in the for loop to define the limit of iterations. That helps to iterate through elements to perform some actions in DOM elements.

The following example shows, using the JavaScript length in the for loop. See demo online and read details below:

Experience this example online



As you can see, the i variable’s maximum limit is set by using array length property in for loop. The USStates array, that contains four US State names is then displayed in HTML paragraph by using the innerHTML method.

The length property returned value 4 and assigned this to the for loop. So for loop will iterate till variable i’s value is less than 4.

JavaScript length to set the size of array example

You can also use length property to set the size of an array. You may require that in specific scenarios to define the size of an array. The following example shows how you can set that.

Experience this example online



You can see, the array length is set to 4. If you use less than four elements in the array and try to display, it will show this as undefined. Similarly, if you use more than four elements while the length is set to 4, then elements will cut off or will not be shown.

Similarly, if you use more than four elements while the length is set to 4, then elements will cut off or will not be shown.

See example below.

An array length set example with more elements

The following example sets the array elements equal to five while we will assign only 3 array elements. See how array will be displayed by using the for loop?

Experience this example online



You can see, the array length is set to five. The elements are then displayed by using the for loop. As there are only three array element values (State names) the other two will be displayed as undefined.

You may also like to read: Javascript Array


Was this article helpful?

Related Articles

Leave A Comment?