Ok. Let’s start with a small and easy to understand code sample.
var a = new Array(1,2,3,4,5); a // returns 1 a.length // returns 5
It is easy to understand what the code above does right? Creates an array of 5 elements, thus the length is 5.
What about the following though?
var a = new Array(10); a a.length
Can you guess what they return?
Well a will obviously return undefined, given that we set no values in the array. But if the array has no values what will length return?
Well it will return the amount of memory we allocated using new Array method.
This array constructor has more than one overloads. The overload in which you have only a single argument, and that argument is an integer, it basically is used as an array size definition.
The constructor of Array took a page from other programming languages that do not use dynamic data manipulation and behave in a more strict way.
int *a = (int *) malloc( 10*sizeof(int) ); // ya ol' c int *a = new int; // c++ int a = new int; // c# or java
Theoretically, symantics are correct with this constructor overload. You are telling the constructor what size the array you want it to be, just like any other language.
For example the following code.
var a=; a.push(2); // puts 2 as the last element of the array, returns length a.pop(); // returns last object of array, removes it a.unshift(30); // puts 30 as the first element of the array, returns length a.shift(); // returns first object of the array, removes it
So what is the problem with new Array(10)? Well the problem comes down to our personal usage. What if I just want to create an array with a single element inside? What if I do the following?
var a = new Array(10); a.push(30);
Well the code above will create an array of 10 items, all of them undefined, and then push an 11th item in it, number 30. So in the end we will end up with an array of 11 items, which is not what we really need. We could either needed 10 items with 30 as the last item, or 2 items, 10 and 30.
We could have needed 2 different items, but we ended up with a 3rd one. So the call to new Array(10) is in fact very ambiguous and usage of it is not really clear.
On the other hand, we could have used the following code, which has clearer intent and results.
var a =  a // returns 10 a.length // returns 1 a.push(30) //returns 2 a.toString() // returns "10,30"
Here our intents are more clear to the reader/reviewer of our code, plus it actually does what we want it to do.