function a(array){
return Array(array.length);
}
console.log(a(["one","two","three"]))
the above snippet gives me this result:
[ , , ,]
a.k.a 3 an array with 3 empty space that is waiting to be filled in.
so Array(3) means make an empty array that has 3 elements inside it, is that correct?
I rarely learn so much from answering a question. My first thought was "he should be calling the Array constructor by using new Array(3)
. Does his code even work? I tested it, and I couldn’t get it to fail. According to this answer on StackOverflow, calling the Array constructor as a function without new
works because it’s in the specification.
So, yes. The following two pieces of code will create an empty array with 3 elements in it:
Array(3);
new Array(3);
1 Like
thank you. I was doing this tutorial online and could not understand the snippet in it,.
1 Like
Also, if you could, mark my original reply with the green checkmark to indicate solved, that would be great.
1 Like