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 slots in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array. It may be greater than the number of elements if the array is sparse.
Discussions

Are rhere any simpler ways to measure length of an array in JS?
Are you okay? More on reddit.com
🌐 r/programminghorror
69
1013
October 25, 2024
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
How come javascript arrays aren't of fixed-length?

Are the dynamic arrays indexed linked lists? Are they fixed arrays that are reinitialized?

No and no. All JavaScript objects are collections of key/value pairs called properties. Keys are strings(*) and values are any type. They're typically implemented as hash maps, but there's no requirement for that. Arrays are just special cases of objects whose keys are strings that look like non-negative integers. That's why arrays can have empty/missing values, as well as no predetermined size. You can even assign non-numeric-looking properties to arrays if you really want:

> var foo = [1, 2, 3];
undefined
> foo.bar = 'blurgh';
'blurgh'
> foo.length
3
> foo[2]
3
> foo['2']
3
> foo['bar']
'blurgh'
> Object.keys(foo)
[ '0', '1', '2', 'bar' ]

This example is meant to demonstrate several things:

  • You can assign arbitrary properties to an object, and an array is still an object.

  • Indexing always converts the argument to a string, i.e. foo[2] and foo['2'] are the same thing.

  • The bar property is no different than the others when asked to list all the keys of the object, and all the keys are strings.

  • For an array, the length property is only updated when assigning a key that is numeric-looking. It doesn't necessarily correspond to how many properties there are.

Note on the last point that length is really just a proxy for "one plus the last seen numeric-looking key." It doesn't have anything to do with the actual length. For example:

> var foo = [];
undefined
> foo[3] = 'abc';
'abc'
> foo
[ , , , 'abc' ]
> foo.length
4

Here foo is an object with a single key, '3'. When asked to display a representation of foo, this REPL chose to do it as [ , , , 'abc' ] which means that the indexes 0 through 2 don't exist. Other implementations may choose a different representation, but the point is that this array only holds one value. (Note: this does not mean that the indexes 0 through 2 just hold undefined or null or some other placeholder; that's a different and distinct case. They don't exist at all.) And yet its length is 4, which is one more than the highest seen index.

So you have to throw away the notion of an array being like an array in other languages; it's really a hash map. But an implementation is free to actually use a real array if you never do any of the things that require hash-map like behavior.

(*) ES6 adds Maps which allow for arbitrary objects as keys.

More on reddit.com
🌐 r/javascript
13
4
November 2, 2015
How to initialize an array's length in JavaScript? - Stack Overflow
Most of the tutorials that I've read on arrays in JavaScript (including w3schools and devguru) suggest that you can initialize an array with a certain length by passing an integer to the Array More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.
🌐
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 ().
🌐
Career Karma
careerkarma.com › blog › javascript › javascript array length: a complete guide
JavaScript Array Length: A Complete Guide | Career Karma
December 1, 2023 - The JavaScript array length property states the number of items in an array. To find the length of an array, reference the object array_name.length.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › Array
Array() constructor - JavaScript | MDN
Thrown if there's only one argument (arrayLength) that is a number, but its value is not an integer or not between 0 and 232 - 1 (inclusive). ... const fruits = ["Apple", "Banana"]; console.log(fruits.length); // 2 console.log(fruits[0]); // "Apple"
Find elsewhere
🌐
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).
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
July 28, 2026 - Other methods (e.g., push(), splice(), etc.) also result in updates to an array's length property. ... When setting a property on a JavaScript array when the property is a valid array index and that index is outside the current bounds of the array, the engine will update the array's length ...
🌐
Udemy
blog.udemy.com › home › it & development › web development › 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
April 14, 2026 - The JavaScript `array.length` property returns the number of elements in an array and can also set the array's maximum size. This article covers array basics, index numbering, and how to use `length` to retrieve values.
🌐
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 - Including its length? ... "Every Array object has a length property whose value is always a nonnegative integer less than 232.
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

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-array-length-property
JavaScript Array length - GeeksforGeeks
June 17, 2026 - The JavaScript array length property returns or sets the number of slots in an array.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Errors › Invalid_array_length
RangeError: invalid array length - JavaScript | MDN
1 month ago - 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.
🌐
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.
🌐
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 - To get the size (or length) of an array in JavaScript, we can use array.length property.
🌐
Reddit
reddit.com › r/javascript › how come javascript arrays aren't of fixed-length?
r/javascript on Reddit: How come javascript arrays aren't of fixed-length?
November 2, 2015 -

(noob alarm) So I have a Java background, a language where arrays have constant lengths, and I'm wondering how languages with dynamic-?- arrays actually implement their arrays (Javascript, Python, Ruby, Swift..etc). I tried to google it but I think I'm using the wrong terms since I couldn't find anything.

Are the dynamic arrays indexed linked lists? Are they fixed arrays that are reinitialized?

I know it doesn't matter but I'm starting to get curious about these under the hood stuff. And yeah, if you couldn't tell.

Top answer
1 of 4
5

Are the dynamic arrays indexed linked lists? Are they fixed arrays that are reinitialized?

No and no. All JavaScript objects are collections of key/value pairs called properties. Keys are strings(*) and values are any type. They're typically implemented as hash maps, but there's no requirement for that. Arrays are just special cases of objects whose keys are strings that look like non-negative integers. That's why arrays can have empty/missing values, as well as no predetermined size. You can even assign non-numeric-looking properties to arrays if you really want:

> var foo = [1, 2, 3];
undefined
> foo.bar = 'blurgh';
'blurgh'
> foo.length
3
> foo[2]
3
> foo['2']
3
> foo['bar']
'blurgh'
> Object.keys(foo)
[ '0', '1', '2', 'bar' ]

This example is meant to demonstrate several things:

  • You can assign arbitrary properties to an object, and an array is still an object.

  • Indexing always converts the argument to a string, i.e. foo[2] and foo['2'] are the same thing.

  • The bar property is no different than the others when asked to list all the keys of the object, and all the keys are strings.

  • For an array, the length property is only updated when assigning a key that is numeric-looking. It doesn't necessarily correspond to how many properties there are.

Note on the last point that length is really just a proxy for "one plus the last seen numeric-looking key." It doesn't have anything to do with the actual length. For example:

> var foo = [];
undefined
> foo[3] = 'abc';
'abc'
> foo
[ , , , 'abc' ]
> foo.length
4

Here foo is an object with a single key, '3'. When asked to display a representation of foo, this REPL chose to do it as [ , , , 'abc' ] which means that the indexes 0 through 2 don't exist. Other implementations may choose a different representation, but the point is that this array only holds one value. (Note: this does not mean that the indexes 0 through 2 just hold undefined or null or some other placeholder; that's a different and distinct case. They don't exist at all.) And yet its length is 4, which is one more than the highest seen index.

So you have to throw away the notion of an array being like an array in other languages; it's really a hash map. But an implementation is free to actually use a real array if you never do any of the things that require hash-map like behavior.

(*) ES6 adds Maps which allow for arbitrary objects as keys.

2 of 4
1

I'm starting to get curious about these under the hood stuff.

This is a great free book.

https://github.com/getify/You-Dont-Know-JS

Your question I think is answered here:

https://github.com/getify/You-Dont-Know-JS/blob/master/types%20&%20grammar/ch1.md

🌐
Medium
sachinkasana.medium.com › node-js-array-limits-whats-the-maximum-length-you-can-reach-ae2d5bdd13a4
Node.js Array Limits: What’s the Maximum Length You Can Reach? | by Sachin Kasana | Medium
August 22, 2024 - To start, a quick look at the documentation reveals that the maximum length of an array in JavaScript (and by extension, Node.js) is 232−12^{32} — 1232−1, which equals 4,294,967,295 elements.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-arrays
JavaScript Arrays - GeeksforGeeks
We can increase and decrease the array length using the JavaScript length property.
Published: 3 weeks ago
🌐
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.