Why do you use array constructor with count of data?

I was just learning JavaScript and came across this. I know JS arrays are dynamically sized, automatically growing to accommodate any data that is added to them. If this is the case, why would you want to write for instance “let icecreamflavors = new Array(20);” ? If you can please give a detailed explanation. This is the place I encountered arrays.

You came across what and where?

It is almost never suggested anywhere to use the Array constructor. Unless maybe if you are using fill (or I guess Typed arrays) you are not likely to use a constructor.

Creating Arrays
Arrays can be created in several basic ways. One is to use the Array constructor, as in this line:
let colors = new Array();
If you know the number of items that will be in the array, you can pass the count into the constructor,
and the length property will automatically be created with that value. For example, the following
creates an array with an initial length value of 20:
let colors = new Array(20);
The Array constructor can also be passed items that should be included in the array. The following
creates an array with three string values:
let colors = new Array(“red”, “blue”, “green”);
An array can be created with a single value by passing it into the constructor. This gets a little bit
tricky because providing a single argument that is a number always creates an array with the given
The Array Type ❘ 171
number of items, whereas an argument of any other type creates a one-item array that contains the
specified value. Here’s an example:
let colors = new Array(3); // create an array with three items
let names = new Array(“Greg”); // create an array with one item, the string “Greg”
It’s possible to omit the new operator when using the Array constructor. It has the same result, as you
can see here:
let colors = Array(3); // create an array with three items
let names = Array(“Greg”); // create an array with one item, the string “Greg”
A second way to create an array is by using array literal notation. An array literal is specified by using
square brackets and placing a comma-separated list of items between them, as in this example:
let colors = [“red”, “blue”, “green”]; // Creates an array with three strings
let names = ; // Creates an empty array
let values = [1,2,]; // Creates an array with 2 items
In this code, the first line creates an array with three string values. The second line creates an empty
array by using empty square brackets. The third line shows the effects of leaving a comma after the
last value in an array literal: values is a two-item array containing the values 1 and 2.

In Professional.JavaScript.for.Web.Developers.4th.Edition

As a rule of thumb we don’t want to write it this way and we should prefer literal notation for any type that has it. As @lasjorg suggested, pretty much the only way when you’re going to use this kind of notation is when you need array of certain size filled with some initial value:


I also meant to ask you which book should I use? I was looking for a physical book (paper) to learn JavaScript. I did a number of Google searches and read reviews but most books seem to presume that the reader knows at least the fundamentals of JavaScript and has some experience tinkering with it. Could you please list some books to learn JavaScript from the ground-up (starting with fundamentals), the book teaches from ground-up and assumes no previous knowledge on the reader’s part except HTML and CSS? In addition, while I will practice writing JavaScript along with reading the book, the book, while starting with basics, should be packed with enough information so that I can build a cool web app by the end of reading it. with Furthermore, if possible could you link a webpage with a good list of books to learn JavaScript for a complete novice, to both programming and JavaScript, having never written code except HTML and CSS. If possible, the book should teach ES6. Please do not suggest Eloquent JavaScript and You Don’t Know JS Yet by Kyle Simpson because I have already taken a look at it and it is a more ‘intermediate’ book in my opinion. The author himself notes - “If you are new to programming or JS, be aware that these books are not intended as a gentle “intro to JavaScript.” This material is, at times, complex and challenging, and goes much deeper than is typical for a first-time learner. You’re welcome here no matter what your background is, but these books are written assuming you’re already comfortable with JS and have at least 6–9 months experience with it.” . Thank you for your help!

Honestly, I’d start with the curriculum here on Free Code Camp so that you can handle more advanced topics. Books struggle to stay up to date and they aren’t as interactive as the forums.

Thanks but I would like @snigo 's suggestions too

I’m sure others will reply with their opinions whenever they have time and have something to say?

I like CS and books about programming in general, I personally think that books about specific language are unnecessary, as language is nothing more than set of functions available for you to program. (Again, IMHO) the best JS “book” is MDN.

I tend to disagree with Kyle Simpson on quite a lot of topics and I would never suggest his books as they are opinionated in unhealthy way. If you need a mentor in your life, instal Eslint.

If you’re looking for great books, here are some: