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 constructor using the var test = new Array(4);
syntax.
After using this syntax liberally in my js files, I ran one of the files through jsLint, and it freaked out:
Error: Problem at line 1 character 22: Expected ')' and instead saw '4'.
var test = new Array(4);
Problem at line 1 character 23: Expected ';' and instead saw ')'.
var test = new Array(4);
Problem at line 1 character 23: Expected an identifier and instead saw ')'.
After reading through jsLint's explanation of its behavior, it looks like jsLint doesn't really like the new Array()
syntax, and instead prefers []
when declaring arrays.
So I have a couple questions. First, why? Am I running any risk by using the new Array()
syntax instead? Are there browser incompatibilities that I should be aware of? And second, if I switch to the square bracket syntax, is there any way to declare an array and set its length all on one line, or do I have to do something like this:
var test = [];
test.length = 4;
I'm surprised there hasn't been a functional solution suggested that allows you to set the length in one line. The following is based on UnderscoreJS:
For reasons mentioned above, I'd avoid doing this unless I wanted to initialize the array to a specific value. It's interesting to note there are other libraries that implement range including Lo-dash and Lazy, which may have different performance characteristics.
This will initialize the length property to 4:
(this was probably better as a comment, but got too long)
So, after reading this I was curious if pre-allocating was actually faster, because in theory it should be. However, this blog gave some tips advising against it http://www.html5rocks.com/en/tutorials/speed/v8/.
So still being unsure, I put it to the test. And as it turns out it seems to in fact be slower.
This code yields the following after a few casual runs:
The array constructor has an ambiguous syntax, and JSLint just hurts your feelings after all.
Also, your example code is broken, the second
var
statement will raise aSyntaxError
. You're setting the propertylength
of the arraytest
, so there's no need for anothervar
.As far as your options go,
array.length
is the only "clean" one. Question is, why do you need to set the size in the first place? Try to refactor your code to get rid of that dependency.Why do you want to initialize the length? Theoretically there is no need for this. It can even result in confusing behavior, because all tests that use the
length
to find out whether an array is empty or not will report that the array is not empty.Some tests show that setting the initial length of large arrays can be more efficient if the array is filled afterwards, but the performance gain (if any) seem to differ from browser to browser.
jsLint does not like
new Array()
because the constructer is ambiguous.creates an empty array of length 4. But
creates an array containing the value
'4'
.Regarding your comment: In JS you don't need to initialize the length of the array. It grows dynamically. You can just store the length in some variable, e.g.
The reason you shouldn't use
new Array
is demonstrated by this code:Some other code could mess with the Array variable. I know it's a bit far fetched that anyone would write such code, but still...
Also, as Felix King said, the interface is a little inconsistent, and could lead to some very difficult-to-track-down bugs.
If you wanted an array with length = x, filled with undefined (as
new Array(x)
would do), you could do this: