One is good and the other is bad :-)
No seriously, the first way is shorter (fewer bytes sent over the wire) and it doesn't have any weird potential problems. The second one, however, will work fine.
The weird part with new Array()
is this: if you pass one numeric parameter to the constructor, it means "initialize the array so that there are that there are the given number of empty (null
) array elements, and so that length
is equal to the given number." (Well it has to be an integer, I think actually.) If you pass a single non-number, or two or more numbers, it means, "initialize the array so that its contents reflect this argument list."
Ok now I said there was a weird part, and it's this: if you're using some sort of server side framework, and you want to create an array of numeric values (integer values), like say unique database keys, then you might be tempted to do this:
var keys = new Array(<@ favoriteTemplateLanguage.forEach(idList): print(id + ',') @>);
(I made up that syntax of course.) Now what happens if there's just one "id" in your server-side list of keys?