Abraham P Abraham P - 1 year ago 108
Javascript Question

Array.size() vs Array.length

What is the difference between the two?

So I know that

is a function while
is a property. Is there a usecase for using one over the other? Is one more efficient (I would imagine
to be significantly faster as it is a property rather then a method call?) Why would one ever use the slower option? Are there some browsers that are incompatible with one or the other?

var x = [];
x =[1,2,3];

Will print:

0, 0, true
3, 3, true

Answer Source

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).