How to find the length of an array in JavaScript?


In this tutorial, we will learn about the method of finding the length of an array in JavaScript. The length of an array is the number of elements or items contained by a JavaScript array. In JavaScript, we have only one way or method to find out the length of an array.

  • Using the length property

Using the length property

In JavaScript, we are allowed to use the length property of JavaScript for finding the length of an array. As we know the length of an array is the number of elements contained by the array, the length property will return a single number that denotes the length of the array or number of elements inside it.

Let us see how we can use the length property to find the length of array in JavaScript.

Syntax

The following syntax will explain how to use the length property to find the length of array in JavaScript −

let arr = [];
let len = arr.length;

Using the above syntax, we can find length of an array, even we can set the length of an array using this syntax.

Steps

  • Step 1 − First of all, we need to define the array whose length we are going to find using JavaScript syntax to define array.
  • Step 2 − In next step, we will use the length as shown in above syntax to find array length or number of elements inside it.
  • Step 3 − The third step will contain the logic of showing the result of above steps, such that length of array comes out from those steps.

Let us understand implementation of above method with the help of code examples.

Example 1

The below example will illustrate the use of length property to find the length of an array in JavaScript −

<html> <body> <h3>Finding the length of an array in JavaScript</h3> <p>The Dummy array is: [25, 38, 36, 20, 35]</p> <p id="result"></p> <script> let arr = [25, 38, 36, 20, 35]; let len = arr.length; document.getElementById('result').innerHTML = "The length of above array is: " + len; </script> </body> </html>

In the above example, we used arr as the dummy array to find the length using the length property in JavaScript.

The below example will explain the use of the length property to find the length of the array in JavaScript when there are some elements missing in the initialization of the array.

Let us see what length will return when there are one or more elements missing in the array −

Example 2

<html> <body> <h3>Find the length of an array in JavaScript</h3> <p>The Dummy array is: [,54,63, ,26,48,10, ,15, ]</p> <p id="result"></p> <script> let arr = [, 54, 63, , 26, 48, 10, , 15]; let len = arr.length; document.getElementById('result').innerHTML = "The length of above array is: " + len ; </script> </body> </html>

In the above example, we can clearly see that there are three elements that are missing in the declaration of the array arr, but still, the length property returns the length of the array as 9, which means it also counts the items that are missing in the declaration as the items contained by it, that is why it is showing the length of the array as 9.

Example 3

Now, in this example, we will see what will be the value returned by the length property if we add or delete one or more items from the array after the declaration of it.

<html> <body> <h3>Find the length of an array in JavaScript</h3> <p>The Dummy array was: ["Potato", "Tomato", "Onion"]</p> <p id="result"></p> <script> let veggies = ["Potato", "Tomato", "Onion"]; veggies.push("Ladyfinger"); veggies.push("Cabbage"); veggies.pop(); let len = veggies.length; document.getElementById('result').innerHTML = "The length of above array is: " + len + "</br>" + "<br>While the array after modification is: " + veggies + ""; </script> </body> </html>

In this example, we declared an array of vegetables using veggies, Where we are modifying the array after its declaration using the push() and pop() methods of JavaScript to add and delete elements of an array respectively, where the pop() method always remove the last element of the array by default. In this case, we are pushing two items in the array as Ladyfinger and Cabbage using the push() method, but later on we are popping an element from the array using the pop() method, which removes the last element which is Cabbage here in this example. Therefore, we are getting the length of the array as 4 and only four elements printed on the screen.

In this tutorial, we have learned how we can find the length of an array in JavaScript using the length property, and also seen how the length property will react or execute in different scenarios or conditions of declaring an array using respective code examples.

Updated on: 31-Oct-2022

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements