How to Find the JavaScript Array Length

How to Find the JavaScript Array Length

6 mins read907 Views Comment
Chanchal
Chanchal Aggarwal
Senior Executive Content
Updated on Dec 18, 2023 16:22 IST

The length property of a JavaScript array returns the number of elements in the array, providing an easy way to determine its size. It's dynamic, meaning it updates as elements are added or removed, and can also be set explicitly to truncate or clear the array. This property is fundamental for iterating over arrays and managing their contents efficiently. 

2022_11_Array-Length-1.jpg

The array’s length, or the number of values in the array, is something you’ll frequently want to know. The JavaScript array length property comes into the role, it's one of the methods of JavaScript. The length property returns how many elements are contained in a specific array.

Let us take the example of a vendor who wants to know how many boxes are there in stock. Using the JavaScript array length property, he can find out how many boxes are available in the warehouse. In this blog, we will learn how to find the length of Javascript array using this property.

Check out: Free JavaScript Courses Online

Table of Contents

 

JavaScript Array Length

The JavaScript array length property is an integer value representing the quantity of items in an array. Since index values in JavaScript begin at 0, the length property is always one more than the highest index value in the array. The length property of an array, is a 32-bit unsigned integer that is always numerically greater than the array’s highest index.

The JavaScript property length does not accept any parameters because it is a property. It adds a new item at the end of an array.

The syntax for the JavaScript property for array length is as follows:


 
Array_name.length;
Copy code

We append the.length syntax to the end of the array’s name to access the length property’s value.

15+ JavaScript Array Methods with Examples
15+ JavaScript Array Methods with Examples
JavaScript array methods offer powerful tools for manipulating arrays. Key methods include 'push()' and 'pop()' for adding/removing elements at the end, 'shift()' and 'unshift()' for operations at the beginning, 'map()'...read more

JavaScript Array Sort
JavaScript Array Sort
The JavaScript array.sort() method sorts the elements of an array in place and returns the sorted array. By default, it sorts elements as strings in ascending order, but a custom...read more

Example:


 
const clothing = ['Mercedes', 'BMW', 'Audi', 'MG Hector'];
console.log(car.length);
Copy code

// expected output:

4

Explanations:

Array Declaration: The cars array is declared with four elements: 'Mercedes', 'BMW', 'Audi', and 'MG Hector'.

Length Property: cars.length refers to the length property of the cars array. This property calculates the number of elements in the array.

Output: When console.log(cars.length); is executed, it prints 4 to the console. This number represents the total count of elements in the cars array, which in this case are four car brands.

The length property is a straightforward way to find out how many items are stored in an array, which is especially useful for looping through arrays or performing operations on their contents.

Recommended online courses

Best-suited JavaScript courses for you

Learn JavaScript with these high-rated online courses

2.05 K
3 months
2.2 K
4 weeks
– / –
40 hours
– / –
40 hours
8.5 K
62 hours
– / –
40 hours
– / –
40 hours
– / –
40 hours

Modification in JavaScript Array Length

The array object observes the length property, automatically synchronizing the length value and array content. The array length property’s value can be changed in JavaScript. You can empty the array of elements or make it sparse by adjusting the length value. This implies:

  • In case the length is set to a value lower than the current value, any elements beyond that are removed from the array.
  • Any index greater than 232 extends the array; the length property is updated to reflect the new maximum index.
  • RangeError exception is thrown when the length is set to an invalid value, such as a negative number or a non-integer.
  • When the length is set to a number greater than the current length, empty slots are added rather than actual undefined items.
Introduction to JavaScript Data Types With Examples
Introduction to JavaScript Data Types With Examples
JavaScript supports several data types including numbers, strings, booleans, arrays, objects, null, and undefined. Numbers represent numeric values, strings represent text, booleans represent true or false values, arrays store multiple...read more

JavaScript Array Push
JavaScript Array Push
The JavaScript array.push() method adds one or more elements to the end of an array, directly modifying the original array. It's widely used for dynamically expanding arrays, and conveniently returns...read more

The following example shows the length property to iterate across the array numbers. Then, each element’s value is doubled.

1. Iterating Through an Array


 
const numbers = [1, 2, 3, 4, 5];
const length = numbers.length;
for (let i = 0; i < length; i++) {
numbers[i] *= 2;
}
Copy code

Output:

// numbers is now [2, 4, 6, 8, 10]

Explanation:

Array Initialization:

The numbers array is initialized with the elements [1, 2, 3, 4, 5].

Getting Array Length:

const length = numbers.length; stores the length of the numbers array (which is 5) in the length variable.
Looping Through the Array:

1. The for loop is set up to iterate over the array. It starts with i = 0 and runs as long as i is less than length (5 in this case).
2. On each iteration, i is incremented by 1 (i++).
Modifying Array Elements:

1. Inside the loop, each element of the array is accessed using numbers[i] and then multiplied by 2.
2. The result of this multiplication replaces the original value at that position in the array (numbers[i] *= 2).
Result:

After the loop completes, each original number in the array has been doubled.
Therefore, the numbers array is transformed into [2, 4, 6, 8, 10].

How to Use JavaScript Array Filter
How to Use JavaScript Array Filter
The JavaScript array.filter() method creates a new array filled with elements that pass a test provided by a function. It's a powerful tool for quickly extracting subsets of data from...read more

How to Remove Duplicates from JavaScript Array?
How to Remove Duplicates from JavaScript Array?
Entering some items twice is a common problem one can face while entering data in a system. It can be duplicates of names while entering students’ names and numbers or...read more

2. Shortening an Array

If the current length is more than 3, the next example reduces the array to a length of 2. 


 
const numbers = [4, 9, 16, 25, 36];
numbers.length = 2;
console.log(numbers);
Copy code

Output:

[4,9]

Explanation:
1. The numbers array is correctly initialized with multiple number elements: [4, 9, 16, 25, 36].
2. By setting numbers.length = 2, the array is truncated, keeping only the first two elements (4 and 9) and discarding the rest.
3. When console.log(numbers); is executed, it prints the truncated array [4, 9], showing the result of this operation.

3. Make an Array of Void

The array is empty if the length is set to 0:


 
const numbers = ['4', '9', '16'];
numbers.length = 0;
console.log(numbers);
Copy code

Output:

[]

Explanation:

Array Initialization:

The numbers array is initialized with three string elements: '4', '9', and '16'.
Setting Array Length to 0:

The line numbers.length = 0; sets the length of the numbers array to 0. This operation effectively removes all elements from the array, making it empty.
Output:

1. The console.log(numbers); statement then outputs the current state of the numbers array.
2. Since the array has been emptied, the output is [], indicating an empty array.

Working with JavaScript Objects
Working with JavaScript Objects
JavaScript is an object-oriented programming language. In JavaScript, everything is an object. A JavaScript object is a stateful entity with behavior (properties and method). For example, a car, a pen,...read more

JavaScript Object Methods- How to Use Them?
JavaScript Object Methods- How to Use Them?
JavaScript methods are actions that can be executed on objects. This article will discuss JavaScript method objects, types of JavaScript methods, and their features. You will learn about its various...read more

Conclusion

The length of a JavaScript array returns the number of elements that are kept in a list. This approach is beneficial when figuring averages and other calculations when knowing the y is necessary.

The number of items in a JavaScript array may be found using the length property covered in this tutorial. Additionally, two examples of the length characteristic were discussed. You can use the length property like a pro after reading this guide!

FAQs

What is the JavaScript method for finding an array's length?

The JavaScript array length property indicates the number of objects in an array. Refer to the object array name to get an array's length. The length property returns an integer.

How do you know the length of an array?

Use the sizeof operator, such as sizeof, to determine an array's size (arr). Using the sizeof function, determine the size of a datatype value (datatype). Divide the overall array size by the size of one of the datatypes you use to get the length of the array (the number of elements).

What is array length 1 in JavaScript?

Length -1 mainly means the -1 portion. We got this when running a for loop over an array: for I = 0; I array.

Can we set the length of the array in JavaScript?

The number of items in an array can be set or returned using JavaScript's array length property. The array length is set using the following syntax. It gives the array's length back.

Is it array length () or array length?

The length() method is relevant to string objects but not arrays, whereas the length variable applies to arrays but not to string objects.

About the Author
author-image
Chanchal Aggarwal
Senior Executive Content

Chanchal is a creative and enthusiastic content creator who enjoys writing research-driven, audience-specific and engaging content. Her curiosity for learning and exploring makes her a suitable writer for a variety ... Read Full Bio