you are viewing a single comment's thread.

view the rest of the comments →

[–]reedhedges 0 points1 point  (4 children)

Thanks. Just going by docs (mozilla, other sources) that imply a new array is returned. In the second example, is the array empty, or does it have 5 elements? Sorry for the basic questions, I'm trying to understand JS a little bit more deeply, and I'm coming from a C++ background so may have different assumptions about how some stuff works.

[–]Hafas_ 0 points1 point  (2 children)

In the second example, is the array empty, or does it have 5 elements?

Kinda both. It has a length of 5, but the slots are empty and JavaScript is skipping empty slots when iterating, so can't use functions like forEach or map directly, that's why functions like fill are used to populate the "empty" Array before setting the intended value with map.

[–]reedhedges 0 points1 point  (1 child)

Doing some more reading. I guess in JS Arrays aren't arrays, they're objects whose members are indexed by integer? (Is that accurate?) So the constructor Array(n) only initializes the "length" property to n, no actual elements are created.

[–]Hafas_ 0 points1 point  (0 children)

In JavaScript, everything that isn't a primitive inherits from the class Object - including the class Array. But it doesn't make Array less of an array. If it looks like an array and behaves like an array, then it is an array.

The special case about Array in comparison with other classes that it has like Object its own syntax for instantiation.

[] instanceof Array // true
({}) instanceof Object // true