Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
one number in new Array(50) is only = array.length
I understand that the array constructor is not recommended, but I find it interesting that when I pass just one number into the parameter of an array constructor, that number is seen as the length of the array. However when I have 2 elements in my array constructor, it is seen as an array of length 2.
In the code below, I have 50 in the parameter of the array constructor, but what if I want 50 to be a value , and not the length of the array constructor, is it possible with array constructors, or is my only choice an array literal. Cheers!
var show = new Array( 50); alert(show.length); </script>
Adam Moore21,956 Points
I believe the way it works is that a single value in the constructor is interpreted as the length of the array, with multiple values each being a value within the array, just like you said. However, to use:
<script> var show = ; alert(show.length); </script>
is really not that bad to have to write. :)