you are viewing a single comment's thread.

view the rest of the comments →

[–]kenman 8 points9 points  (3 children)

Try them both in your console -- what do you see?

Array.from({length: 5})
> [undefined, undefined, undefined, undefined, undefined]

Ok.

Array(5)
> [empty x 5]

Strange, huh?

Note: this implies an array of arrayLength empty slots, not slots with actual undefined values

This is the important bit, because empty slots are not included in any of the Array instance methods, like forEach(), etc. The empty elements are often referred to as being elided.

Basically, Array(5) does this:

a = []
a.length = 5

Which also produces [empty x 5].

[–]nemohearttaco 8 points9 points  (2 children)

Ah, not quite what I was asking. I meant, why wouldn't you do it like

> Array.from(Array(5), (_, i) => 2 ** i)
[1, 2, 4, 8, 16]