We can use JavaScript to get the length of an array easily by simply using the JavaScript Array length property.

arr.length

And here is a simple example.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

console.log(numbers.length);

#Output
9

When working with arrays, it can be useful to be able to easily calculate the array length and size of an array.

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

Let’s see the length property used on a bunch of different arrays.

var numbersArray = [32,1,0,09,23,430,1000];
var stringsArray = ["This","an","array","of","strings"];
var mixedArray = ["Hello","",null,"null",true,234,"23"];
var arr1 = [];
var arr2 = [""];

console.log(numbersArray.length);
console.log(stringsArray.length);
console.log(mixedArray.length);
console.log(arr1.length);
console.log(arr2.length);

#Output
7
5
7
0
1

Using a For Loop to Get the length of Array in JavaScript

Another way we can easily get the length of an array is with a for loop.

Below shows you how to get the length of an array of numbers in JavaScript with a for loop.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

var arrayLength = 0;
for( var i =0; i

Hopefully this article has been useful for you to learn how to use JavaScript to get the length of array.

Categorized in:

JavaScript,

Last Update: March 11, 2024