Array.size() is not a valid method

Always use the length property

There is a library or script adding the size method to the array prototype since this is not a native array method. This is commonly done to add support for a custom getter. An example of using this would be when you want to get the size in memory of an array (which is the only thing I can think of that would be useful for this name).

Underscore.js unfortunately defines a size method which actually returns the length of an object or array. Since unfortunately the length property of a function is defined as the number of named arguments the function declares they had to use an alternative and size was chosen (count would have been a better choice).

Answer from Gabriel on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › length
Array: length - JavaScript | MDN
The length data property of an Array instance represents the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.
🌐
W3Schools
w3schools.com › jsref › jsref_length_array.asp
JavaScript Array length Property
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST ... Array[ ] Array( ) at() concat() constructor copyWithin() entries() every() fill() filter() find() findIndex() findLast() findLastIndex() flat() flatMap() forEach() from() includes() indexOf() isArray() join() keys() lastIndexOf() length map() of() pop() prototype push() reduce() reduceRight() rest (...) reverse() shift() slice() some() sort() splice() spread (...) toReversed() toSorted() toSpliced() toString() unshift() values() valueOf() with() JS Boolean
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
Why is getting the length of an array O(1) and not O(n)?
Most implementations store the length of an array in the object itself. Whenever you insert/remove it gets updated. Thus you're not iterating to get the length you're just getting a value. More on reddit.com
🌐 r/learnprogramming
45
11
November 7, 2022
Are rhere any simpler ways to measure length of an array in JS?
🌐 r/programminghorror
69
1012
October 25, 2024
How JavaScript Array Works Internally?
I'm dubious of anything that claims performance without actual benchmarks. More on reddit.com
🌐 r/javascript
43
173
January 29, 2022
🌐
JavaScript in Plain English
javascript.plainenglish.io › array-length-can-lead-to-unexpected-errors-ce4fbfa74fc5
Array. length Can Lead to Unexpected Errors | JavaScript in Plain English
July 8, 2022 - In JavaScript, arrays are not primitives but are instead objects. This means that the array’s square bracket declaration probably works as a syntactical sugar to declare an array, but actually, behind the scenes, an object gets created with indexes (0-based) as keys and the array content as respective values. Suppose we delete an element from your array using 𝚍𝚎𝚕𝚎𝚝𝚎 𝚊𝚛𝚛[𝚒] but now if we run arr.length, we would get 4 — like whaaaat?
🌐
Medium
devnewbs.medium.com › basics-of-javascript-array-length-property-b1d851867abe
Basics of Javascript · Array · length (property) | by Jakub Korch
December 8, 2023 - There are no numerical properties when we call the Object.keys() method, but at the same time the length property is set. The reason is simple — in this case, the thing that sets the property length is an Array constructor method which takes the only argument passed and sets it as a length of the newly created array.
🌐
Udemy
blog.udemy.com › home › javascript array length: what it’s about and how to use it
JavaScript Array Length: What It's About and How to Use It - Udemy Blog
December 4, 2019 - Our array had three elements in it, so its length was three. We could have, however, increased the length of the array without adding new elements to it and then printed the length. In that case, we would have received a bigger number as our output. Take this course to learn how to write your own JavaScript programs.
Find elsewhere
🌐
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 ().
🌐
TutorialsPoint
tutorialspoint.com › home › javascript › javascript array length
JavaScript Array Length
September 1, 2008 - 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.
🌐
Codingcreativo
codingcreativo.it › javascript-array-length
JavaScript array length, lunghezza array, proprietà length
November 7, 2022 - This property is also used to determine the length of a string. The syntax is very simple: ArrayName.length.
🌐
CodingNomads
codingnomads.com › javascript-array-length
JavaScript Array Length Property
By mastering the use of .length, ... is incredibly straightforward thanks to the .length property. This property returns the total number of elements in the array....
🌐
CoreUI
coreui.io › answers › how-to-get-the-length-of-an-array-in-javascript
How to get the length of an array in JavaScript · CoreUI
September 19, 2025 - The length property returns the number of elements in the array as an integer. In this example, fruits.length returns 3 because there are three elements in the array. This property is automatically updated whenever elements are added or removed ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Errors › Invalid_array_length
RangeError: invalid array length - JavaScript | MDN
July 8, 2025 - The maximum allowed array length depends on the platform, browser and browser version. For Array the maximum length is 232-1. For ArrayBuffer the maximum is 231-1 (2GiB-1) on 32-bit systems.
🌐
Codecademy
codecademy.com › docs › javascript › storage › .length
JavaScript | Storage | .length | Codecademy
July 8, 2025 - In JavaScript, the .length property is used to determine the number of elements, characters, or items in a given data structure, such as arrays or strings.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-array-length-tutorial
JavaScript Array Length – How to Find the Length of an Array in JS
September 4, 2024 - You can use this to check if an array is empty and, if not, iterate through the elements in it. Javascript has a <.length> property that returns the size of an array as a number(integer).
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › generics.html
TypeScript: Documentation - Generics
Since we’re working with arrays, the .length member should be available.
🌐
Flexiple
flexiple.com › javascript › javascript-length
How to use Javascript Length on Arrays and Strings? - Flexiple
March 14, 2022 - While using Javascript length on an array, the function returns the number of elements in the array.
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

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

🌐
Dustin John Pfister
dustinpfister.github.io › 2018 › 12 › 14 › js-array-length
Array length in javaScript and addressing the confusion | Dustin John Pfister at github pages
November 30, 2021 - One way of thinking about array length might be that Array length in javaScript refers to the highest numbered index value of an array plus one because array length is one rather than zero relative.
🌐
W3Schools
w3schools.com › js › js_arrays.asp
JavaScript Arrays
The real strength of JavaScript ... methods are covered in the next chapters. The length property of an array returns the length of an array (the number of array elements)....