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).
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).
.size() is not a native JS function of Array (at least not in any browser that I know of).
.length should be used.
If
.size() does work on your page, make sure you do not have any extra libraries included like prototype that is mucking with the Array prototype.
or
There might be some plugin on your browser that is mucking with the Array prototype.
Difference between length and size?
Are rhere any simpler ways to measure length of an array in JS?
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]andfoo['2']are the same thing. -
The
barproperty 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
lengthproperty 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!! vs ==0 when checking if array is empty
Click the button to change the number of visible options in the dropdown list.
Try it ```html