The JavaScript array length property states the number of items in an array. To find the length of an array, reference the object array_name.length. The length property returns an integer.
You’ll often want to know how many values are in the array—in other words, the length of the array. That’s where the JavaScript array length property comes in. The length property is used to retrieve the number of items held in a particular array.
For instance, say you are a book salesperson and you want to know how many books are in your inventory. You could retrieve the length of your book list using the JavaScript array length property.
This tutorial will explore how to use the array length method in JavaScript. We will walk through two examples that use the method in a JavaScript program.
JavaScript Array length
The JavaScript array length property is an integer value that represents the number of items in an array. The length property is always one higher than the highest index value in the array because index values start from 0 in JavaScript.
Since length is a property in JavaScript, it does not take in any parameters. It is simply appended to the end of an array.
Here is the syntax for the JavaScript array length property:
array_name.length;
We add the .length syntax to the end of an array name to access the value of the length property.
Array length JavaScript Example
Let’s walk through an example to illustrate this method in a JavaScript program. Suppose that we own a bookstore. We want to find out how many books from the New York Times Fiction Best Sellers of 2019 list we sell. One customer inquired about the availability of these books, so we decided to check how many books we sell that appeared on the list.
That’s where the length property can be useful. Let’s say we have a list of books we sell that appeared on the New York Times Fiction Best Sellers of 2019 list. We could find out how many books we sell are on that list using the length property:
const nyt_fiction_best_sellers_2019 = ['Redemption', 'A Better Man', 'The Institute', 'The Guardians', 'The Night Fire', 'Turning Point']; console.log(nyt_fiction_best_sellers_2019.length);
When we execute our code, the program returns: 6.
First, we declare a constant, called nyt_fiction_best_sellers_2019. This constant stores a list of the books in our inventory that appeared on the New York Times Fiction Best Sellers of 2019 list. Then, we use the length property to find out the length of that list. In this case, there are six values in our list, so the length property returns: 6.
Calculating Averages with JavaScript Array length
The length property has a number of use cases in JavaScript, and one of the most common of these is calculating averages.
Suppose that we wanted to find the average age of members in a dance class. We have a list that stores the dancers’ ages. Later, we decide that we want the average to be rounded to the nearest integer. We would need to use the length property to retrieve the number of items in the list. This value will help us calculate an average.
Example Program
Here’s an example of a program that would allow us to calculate the average age of members in the dance class:
var dancer_ages = [9, 10, 9, 11, 12, 8, 10, 10, 10]; var sum = 0; for (var i = 0; i < dancer_ages.length; i++) { sum += dancer_ages[i] } var average = Math.round(sum/dancer_ages.length); console.log("The average age members in the dance class is: " + average + ".")
Our code returns:
The average age of members in the dance class is: 10.
On the first line of code, we declare a JavaScript list called dancer_ages which stores the ages of each member in the dance class. Then, we declare a variable called sum which stores the total age of members in the class. We will need this value to calculate the average later on.
Initially, we set the value of sum to 0 because we need to declare a variable before it can be used. Then, we add the age of each dancer to the sum value in our for loop.
Then, we create a JavaScript for loop that iterates through each item in the dancer_ages list and adds it to the sum variable. Notice that we use the dancer_ages.length property to tell our program how many times it should execute.
Following the execution of the for loop, our program calculates the average age of members in the class. Our program does this by dividing sum (the sum of all dancers’ ages) by dancer_ages.length (the number of people in the dance class). Then, we use the Math.round() method to round our value to the nearest integer.
Finally, we print out a message to the console that states: “The average age of a member in the dance class is:”. This statement is followed by the value stored in the average JavaScript variable, and a period (.).
Conclusion
The JavaScript array length property is used to retrieve the number of items stored in a list. This method is especially in calculating averages and other sums that require knowing the length of a list.
This tutorial discussed how to use the length property to retrieve the number of items in a JavaScript array. We also walked through two examples of the length property. After reading this tutorial, you are ready to start using the length property like an expert!
Do you want to learn more about coding in JavaScript? Read our How to Learn JavaScript guide. You’ll find advice on which courses and learning resources may help you advance your knowledge of JavaScript.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.