What you are looking for is not the length of an array but the number values allocated in that array.

Array.length will NOT give you that result but the total number of values allocated.

A workarround is to count the properties of the object behind the array, with:

Object.keys(a).length

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Relationship_between_length_and_numerical_properties

But with some caveats:

  • It will also count literal properties, like a.a_property. I do not think that is what you want. So, you will have to filter that result:

!(+el % 1) which check if el can be considered as numerical property even if it has a type of string.

  • you want count only positive integers, so you have to filter them with:

+el>=0

  • finally, as array size is limited to 2^32, you will to also filter positive integers greater than that:

+el < Math.pow(2,32)

Functionally, you will have your result with this filter:

Array.realLength= Object.keys(a).filter(function(el){return !(+el % 1) && +el>=0 && +el < Math.pow(2,32) ;}).length 
Answer from Gaรซl Barbin on Stack Overflow
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ javascript-array-length-tutorial
JavaScript Array Length โ€“ How to Find the Length of an Array in JS
September 4, 2024 - let numbers = [12,13,14,25] for (i = 0; i < numbers.length; i++){ console.log(numbers[i]); } # Output # 12 # 13 # 14 # 25 ยท In this method, we will iterate through the elements and count each of the elements present in the array. ... function arrayLength(arr) { let count = 0; for (const element of arr) { count++; } return count; } let numbers = [12,13,14,25] console.log("Length of array:", arrayLength(numbers)); # Output # Length of array: 4
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ length
Array: length - JavaScript - MDN Web Docs
May 2, 2026 - Setting length to a value smaller than the current length truncates the array โ€” elements beyond the new length are deleted. Setting any array index (a nonnegative integer smaller than 232) beyond the current length extends the array โ€” the length property is increased to reflect the new highest index.
Discussions

Find Array length in Javascript - Stack Overflow
This will sound a very silly question. How can we find the exact length of array in JavaScript; more precisely i want to find the total positions occupied in the array. I have a simple scenario w... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Are rhere any simpler ways to measure length of an array in JS?
๐ŸŒ r/programminghorror
69
1013
October 25, 2024
Can I Use a Boolean Test on Array.length?
Yes, length is a number so if it's 0 it evaluates to false. It doesn't look like you need it here, though -- the arguments object returns an array of the arguments passed to the function but since you're assigning optStr to _name, having more than one argument is useless. I think what you want to do is: return (optStr === undefined) // true if no argument was passed ? this._name : (this._name = optStr); More on reddit.com
๐ŸŒ r/learnjavascript
8
2
December 5, 2021
How does arr[arr.length-1] work?
let arr=[1,2]; console.log(arr.length) // 2, 2 elements console.log(arr[0]) // data 1, position 0 console.log(arr[1]) // data 2, position 1 console.log(arr[2])) // error, beyond array scope console.log(arr[arr.length])); // error, beyond array scope console.log(arr[arr.length-1]); // 2 you can also just let last=array.pop(); More on reddit.com
๐ŸŒ r/learnjavascript
31
40
September 12, 2021
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ javascript โ€บ array-length
JavaScript Array Length: Master Data Handling
Quick Answer: How to Get the Length of an Array in JS To get the number of elements in a JavaScript array, you use the .length property. It is a property, not a method, so you do not use parentheses (). It returns an integer representing the count of elements.
๐ŸŒ
CodingNomads
codingnomads.com โ€บ javascript-array-length
JavaScript Array Length Property
By mastering the use of .length, ... straightforward thanks to the .length property. This property returns the total number of elements in the array....
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ javascript โ€บ storage โ€บ .length
JavaScript | Storage | .length | Codecademy
July 8, 2025 - Yes. JavaScript arrays can be sparse, and .length will count the total index range, not just defined elements: ... Modifying .length truncates or extends the array. ... Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ javascript โ€บ array_length.htm
JavaScript - Array length Property
The JavaScript Array.length property is used to return the number of elements present in an array. For instance, if the array contains four elements, then the length property will return 4.
Top answer
1 of 6
28

What you are looking for is not the length of an array but the number values allocated in that array.

Array.length will NOT give you that result but the total number of values allocated.

A workarround is to count the properties of the object behind the array, with:

Object.keys(a).length

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Relationship_between_length_and_numerical_properties

But with some caveats:

  • It will also count literal properties, like a.a_property. I do not think that is what you want. So, you will have to filter that result:

!(+el % 1) which check if el can be considered as numerical property even if it has a type of string.

  • you want count only positive integers, so you have to filter them with:

+el>=0

  • finally, as array size is limited to 2^32, you will to also filter positive integers greater than that:

+el < Math.pow(2,32)

Functionally, you will have your result with this filter:

Array.realLength= Object.keys(a).filter(function(el){return !(+el % 1) && +el>=0 && +el < Math.pow(2,32) ;}).length 
2 of 6
5

TL;DR The simplest reliable approach that I can think of is the following:

var count = a.filter(function() { return true; }).length;

In modern JavaScript engines, this could be shortened to:

var count = a.filter(() => true).length;


Full answer:

Checking against undefined isn't enough because the array could actually contain undefined values.

Reliable ways to find the number of elements are...

Use the in operator:

var count = 0;
for (var i = 0; i < a.length; i += 1) {
    if (i in a) {
        count += 1;
    }
}

use .forEach() (which basically uses in under the hood):

var a = [1, undefined, null, 7];
a[50] = undefined;
a[90] = 10;

var count = 0;
a.forEach(function () {
    count += 1;
});

console.log(count);    // 6
Run code snippetEdit code snippet Hide Results Copy to answer Expand

or use .filter() with a predicate that is always true:

var a = [1, undefined, null, 7];
a[50] = undefined;
a[90] = 10;

var count = a.filter(function () { return true; }).length;

console.log(count);    // 6
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_length_array.asp
JavaScript Array length Property
The length property sets or returns the number of elements in an array. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ยท If you want to report an error, or ...
๐ŸŒ
Flexiple
flexiple.com โ€บ javascript โ€บ javascript-length
How to use Javascript Length on Arrays and Strings? - Flexiple
The length function in Javascript is used to return the length of an object. And since length is a property of an object it can be used on both arrays and strings. Although the syntax of the length function remains the same, bear in mind that ...
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ library โ€บ array โ€บ length
JavaScript Array length
The length property returns or sets the number of elements in an array. let city = ["California", "Barcelona", "Paris", "Kathmandu"]; // find the length of the city array let len = city.length; console.log(len); // Output: 4 ... Here, arr is an array. var companyList = ["Apple", "Google", ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript-array-length-property
JavaScript Array length | GeeksforGeeks
November 16, 2024 - In JavaScript arrays, it returns the function Array(){ [native code] }.Syntax: array.constructorReturn ... JavaScript array length property is used to set or return the number of elements in an array.
๐ŸŒ
Edureka
edureka.co โ€บ blog โ€บ array-length-in-javascript
Array Length In JavaScript | JavaScript Array Length Property | Edureka
February 25, 2025 - The value returned is an unsigned integer. The length property can be specified as: ... <script type="text/javascript"> var music = new Array(); music[0] = "Rock"; music[1] = "Pop"; music[2] = "Jazz"; music[3] = "Blues"; document.write(music.length); </script> ... </p> <script> //JavaScript to illustrate length property function fun() { // length property for array document.write([9,2,4,8,1,7,6,3,5].length); document.write("<br>"); // length property for string document.write("HelloWorld".length) } fun(); </script> <p style="text-align: justify;">
๐ŸŒ
Codersvibe
codersvibe.com โ€บ home โ€บ javascript โ€บ javascript array length method explained with examples
How to find JavaScript array length? | "Coders Vibe"
March 9, 2024 - There are three quick methods to find the length of a JavaScript array: using the direct length property, employing for of and for in loops, and utilizing the concise reduce() function.
๐ŸŒ
Reddit
reddit.com โ€บ r/programminghorror โ€บ are rhere any simpler ways to measure length of an array in js?
r/programminghorror on Reddit: Are rhere any simpler ways to measure length of an array in JS?
October 25, 2024 - I would use the function keyword instead of const and lambda, to make it easier to read for new programmers ... This outputs the wrong value for sparse arrays, by the way. console.log ( Array (4) . length) console.log ( len ( Array (4))) >> 4 >> 0
๐ŸŒ
Career Karma
careerkarma.com โ€บ blog โ€บ javascript โ€บ javascript array length: a complete guide
JavaScript Array Length: A Complete Guide | Career Karma
December 1, 2023 - Finally, we print out a message ... and a period (.). The JavaScript array length property is used to retrieve the number of items stored in a list....
๐ŸŒ
JavaScript Tutorial
javascripttutorial.net โ€บ home โ€บ javascript array methods โ€บ javascript array length
JavaScript Array Length Property
November 4, 2024 - For dense arrays, you can use the length property to get the number of elements in the array. For example: let colors = ['red', 'green', 'blue']; console.log(colors.length); // 3Code language: JavaScript (javascript)
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ javascript-array-length
JavaScript Array Length Explained
January 12, 2020 - length is a property of arrays in JavaScript that returns or sets the number of elements in a given array. The length property of an array can be returned like so. let desserts = ["Cake", "Pie", "Brownies"]; console.log(desserts.length); // ...
๐ŸŒ
Flexiple
flexiple.com โ€บ javascript โ€บ how-to-find-javascript-array-length
How To Find JavaScript Array Length - Flexiple
April 29, 2024 - This property returns a numerical ... length of an array in JavaScript. The length property of the array returns the number of elements in the JavaScript array....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-find-the-length-of-an-array-in-javascript
How to Find the Length of an Array in JavaScript ? - GeeksforGeeks
July 23, 2025 - The spread operator (...) can be utilized along with a function like Math.max to find the maximum index, effectively giving the length of the array.