How to Get the Length of an Array in JavaScript?

Sometimes you need to know the number of elements in that array, but how to get the length of an array in JavaScript?

That is one of the questions that pops up in your mind, right?

That is why the JavaScript array length property comes in, and luckily, you found this article.

It is because, in this article, will walk you through the whole process of how to find length in JavaScript Array.

But before that, let’s understand the JavaScript Array length Property.

What is JavaScript Array length?

The JavaScript array length property provides information about the number of values contained within an array. In simple words, it indicates the size of the array.

In addition to that, the length property of an Array in JavaScript represents the number of elements in that array.

It is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.

It simply means that the maximum value of the length property is 2^32, which means that an array can hold up to 4294967296 elements.

The length property can be used to get or set the number of elements in an array.

For instance, you can use it to iterate over the elements of an array, shorten an array by setting its length to a smaller value, or create an empty array of a fixed length.

Here is an example of how you can access the length property of an array:

let sampleArray = [10, 20, 30, 40, 50];
console.log(sampleArray.length);

In our example code, we create an array called sampleArray with five elements. Then, we use the length property to access the number of elements in the array and log it to the console.

Output:

5

You can also modify the length property directly to change the number of elements in an array.

Here is an example:

let sampleArray = [10, 20, 30, 40, 50];
sampleArray.length = 3;
console.log(sampleArray); 

In our second example, we create the same array as above called sampleArray with five elements. Then, we just set the length property to 3, which removes the last two elements from the array.

Output:

[ 10, 20, 30 ]

The length property is a useful tool for working with arrays in JavaScript.

It allows you to easily access and manipulate the number of elements in an array, making it easier to work with arrays in your code.

How to specify array length in JavaScript?

You can specify the length of an array in JavaScript, by setting its length property to the desired value.

Here is an example:

let sampleArray = new Array(10); // it creates an empty array with a length of 10
console.log(sampleArray.length); 

As you can see, we create a new array called sampleArray using the Array constructor and pass in a single argument, which specifies the length of the array.

Then, we use the length property to access the number of elements in the array and log it to the console.

Output:

10

You can also specify the length of an array by assigning values to its elements.

Here is an example:

let sampleArray = [];
sampleArray[12] = "itsourcecode";
console.log(sampleArray.length);

In our second example, we create an empty array called sampleArray and assign a value to its thirteen element (index 12).

This automatically sets the length of the array to 13, since the highest index in the array is 12.

Output:

13

How to find or get the length of array in JavaScript?

The most common and straightforward way to find or get the length of an array in JavaScript is by accessing its length property.

The length property is a built-in property of arrays that represents the number of elements in the array.

Here’s an example:

let sampleArray = [100, 200, 300, 400, 500];
console.log(sampleArray.length);

Output:

5

Another way to find the length of an array is by using the Object.keys() method.

This method returns an array of an object’s own enumerable properties, which can be used to find the length of an array.

Here is an example:

let sampleArray = [100, 200, 300, 400, 500];
console.log(Object.keys(sampleArray).length);

We create an array called sampleArray with four elements.

Then, we use the Object.keys() method to get an array of the array’s own enumerable properties and use its length property to find the number of elements in the original array.

Output:

5

How to get length of an array string in JavaScript?

To get the length of an array of strings in JavaScript, you can use the length property of the array.

Here’s an example:

let sampleArray = ["it", "source", "code"];
console.log(sampleArray.length); ✅  

As you can see, we have created an array called sampleArray with three string elements.

Then, we use the length property to access the number of elements in the array and log it to the console.

Output:

3

How to check length of an array in for loop in JavaScript?

You can check the length of an array in a for loop in JavaScript by using the length property of the array in the loop’s condition.

Here’s an example:

let sampleArray = [1, 2, 3, 4, 5];
for (let i = 0; i < sampleArray.length; i++) {✅ 
console.log(sampleArray[i]);
}

In this example, we create an array called sampleArray with five elements. Then, we use a for loop to iterate over the elements of the array.

In the loop’s condition, we use the length property of the array to determine how many times the loop should run.

The loop will run once for each element in the array, and on each iteration, it will log the current element to the console.

Output:

1
2
3
4
5

Conclusion

In conclusion, this article discusses the length of an array in JavaScript, which is a handy tool that helps you know how many elements are in an array.

It’s like a counter that tells you the size of your array. You can use it for several tasks, such as going through array items or making arrays shorter.

Knowing the length of your arrays makes your JavaScript coding smoother and more efficient. So, use this knowledge to make your array-handling skills even better.

We are hoping that this article provides you with enough information that helps you understand the length of an array in JavaScript. 

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading itsourcecoders 😊.

Leave a Comment