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
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.
🌐
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
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
As a result, '2' and '02' would refer to two different slots on the years object, and the following example could be true: ... Only years['2'] is an actual array index. years['02'] is an arbitrary string property that will not be visited in array iteration. A JavaScript array's length property ...
🌐
TutorialsPoint
tutorialspoint.com › home › javascript › javascript array length
JavaScript Array Length
September 1, 2008 - The length property of an array in JavaScript returns the number of elements in the array. In the following example, we are using the JavaScript Array.length property to calculate the length of the specified array.
🌐
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).
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Arrays
Let’s say we want the last element of the array. Some programming languages allow the use of negative indexes for the same purpose, like fruits[-1]. However, in JavaScript it won’t work. The result will be undefined, because the index in square brackets is treated literally. We can explicitly calculate the last element index and then access it: fruits[fruits.length - 1].
🌐
Career Karma
careerkarma.com › blog › javascript › javascript array length: a complete guide
JavaScript Array Length: A Complete Guide | Career Karma
December 1, 2023 - Our program does this by dividing sum (the sum of all dancers’ ages) by dancer_ages.length (the number of people in the dance class). Then, we use the Math.round() method to round our value to the nearest integer. Finally, we print out a message to the console that states: “The average age of a member in the dance class is:”. This statement is followed by the value stored in the average JavaScript variable, and a period (.). The JavaScript array length property is used to retrieve the number of items stored in a list.
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 ().
🌐
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); // ...
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

🌐
Codedamn
codedamn.com › news › javascript
Array Length in JavaScript – How to Find the Length of an Array in JS
February 5, 2024 - Adding elements to an array (e.g., ... its length. This dynamic resizing is a crucial feature of JavaScript arrays and one that makes them so versatile and powerful in various programming contexts. Arrays are fundamental structures in JavaScript, providing a versatile way to organize and manipulate data. Here, we’ll delve into some practical examples to understand ...
🌐
Programiz
programiz.com › javascript › library › array › length
JavaScript Array length
Here, arr is an array. var companyList = ["Apple", "Google", "Facebook", "Amazon"]; console.log(companyList.length); // Output: 4 var randomList = ["JavaScript", 44];
🌐
Flexiple
flexiple.com › javascript › how-to-find-javascript-array-length
How To Find JavaScript Array Length - Flexiple
In the above code, dataPoints.length retrieves the count of entries in the array, which is then stored in numberOfDataPoints. The length method applies universally, regardless of the array's content or size. JavaScript arrays provide the length attribute directly, eliminating the need for additional functions or methods to determine the size.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript-array-length-property
JavaScript Array length | GeeksforGeeks
November 16, 2024 - JavaScript array length property is used to set or return the number of elements in an array. JavaScriptlet a = ["js", "html", "gfg"]; console.log(a.length);Output3 Setting the Length of an ArrayThe length property can also be used to set the ...
🌐
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 - Use the length property to get ... array. const fruits = ['apple', 'banana', 'orange'] const count = fruits.length // Result: 3 · 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...
🌐
CodingNomads
codingnomads.com › javascript-array-length
JavaScript Array Length Property
By mastering the use of .length, you'll enhance your ability to handle arrays efficiently in various programming scenarios. Determining the size of an array in JavaScript is incredibly straightforward thanks to the .length property. This property returns the total number of elements in the array.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-get-the-size-of-an-array-in-javascript
How to Get the Size of an Array in JavaScript - GeeksforGeeks
July 23, 2025 - // Defining an array const a = [ 10, 20, 30, 40, 50 ] // Storing length of array let l = a.length; // Displaying the length of arr1 console.log("Length of the Array is:", l); ... The reduce() method iterates over each element of an array, ...