0

I expect this JS:

(new Array(3)).map(_ => 3.14)

... to yield [3.14, 3.14, 3.14]. Instead, it yields [ <3 empty items> ]. My questions:

  1. What on earth "is" this weird structure I've created? It would make sense to me if it were e.g. [undefined, undefined, undefined], but I don't know of a primitive JS structure called <empty item>. It's clearly not the same as [undefined, undefined, undefined], because the latter would have yielded [3.14, 3.14, 3.14].

  2. How can I succinctly create an array [3.14, 3.14, 3.14]?

2
  • 1
    new Array(3).fill(3.14) Commented Apr 9, 2020 at 15:39
  • you can do new Array(3).fill(3.14). If not you can fill it with undefined and then use .map like new Array(3).fill(undefined).map Commented Apr 9, 2020 at 15:41

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.