0

Setting number of elements of arrays

In the Javascript course, a lesson says you can use the syntax "var arr = new Array(3);" to declare an array and tell it how many elements with contain, then populate it. But Array constructor can take a list of elements to populate it on creation. Is there an exception when the constructor takes just one whole number? Because that syntax looks like a valid way to create an array with only one element: the number 3.

11th Oct 2019, 9:24 AM
Mellogangster
Mellogangster - avatar
1 Antwort
0
If you declare more than one parameter when creating array. Example. var a = new Array( 1, 2 ); It's is seen as an Array constructor. Declaring a single parameter like in your example will been seen as an array with 3 empty index's. To avoid getting confused or forgetting. You can use this to kinda visually separate the syntax in a sense. var arr = []; arr.length = 3;
11th Oct 2019, 11:14 AM
Anthony Johnson
Anthony Johnson - avatar