Question

If I want to make an array that is say of length 3 using bracket notation, should I just write: var foo = [,,];

I was more used to writing: var foo = new Array(3);

I noticed that if I removed one , that my code still worked which is surprising because I am accessing all 3 elements after assigning them. How is it that it would still work?

Was it helpful?

Solution

if u do

a = []
a[3] = 100

the indices 0,1,2 will be filled in with undefined for u. u do not have to set a specific array length before using certain indices. array will grow as u use it.

all these are the same:

a = [,,,]

b = []
b.length = 3

c = new Array(3)

d = []
d[2] = undefined
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top