# Is there any better way to create a multidimensional array in Javascript

**URL:** <https://es.discourse.group/t/is-there-any-better-way-to-create-a-multidimensional-array-in-javascript/944>\
**Category:** I have questions\
**Created:** [August 24, 2021, 2:28pm UTC](https://es.discourse.group/t/is-there-any-better-way-to-create-a-multidimensional-array-in-javascript/944 "2021-08-24T14:28:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZhengHu](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/zhenghu/32/970_2.png) [@ZhengHu](https://es.discourse.group/u/ZhengHu)\
**Post date:** [August 24, 2021, 2:28pm UTC](https://es.discourse.group/t/is-there-any-better-way-to-create-a-multidimensional-array-in-javascript/944/1 "2021-08-24T14:28:54Z")

</div>

In C++, creating a 2D array is really simple

```javascript
int two_d[n][m];

```

But after I google how to achieve the same function in javascript, it seems much more complex.

- Array.from() method\*

```javascript
let arr = Array.from(Array(m), () => new Array(n));

```

- Array.prototype.map()Method\*

```javascript
let arr = Array(m).fill().map(() => Array(n));

```

Does Javascript support a similar syntax like C++ which will be much simpler and more visible?

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [August 24, 2021, 3:04pm UTC](https://es.discourse.group/t/is-there-any-better-way-to-create-a-multidimensional-array-in-javascript/944/2 "2021-08-24T15:04:02Z")

</div>

Nope.

---

<div class="post-metadata">

**Author:** ![ZhengHu](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/zhenghu/32/970_2.png) [@ZhengHu](https://es.discourse.group/u/ZhengHu)\
**Post date:** [August 24, 2021, 3:55pm UTC](https://es.discourse.group/t/is-there-any-better-way-to-create-a-multidimensional-array-in-javascript/944/3 "2021-08-24T15:55:04Z")

</div>

I am confused why JS doesn't support simple syntax like C++. You know, when we want to create a multidimensional array in JS, the above ways will become very long and ugly. Maybe I should go to post a proposal for it?

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [August 24, 2021, 5:39pm UTC](https://es.discourse.group/t/is-there-any-better-way-to-create-a-multidimensional-array-in-javascript/944/4 "2021-08-24T17:39:49Z")

</div>

JS doesn't have mutlidimensional arrays - it only has "arrays of arrays". I'm not sure why there's no simple syntax to create them, but I've not found them commonly used in my experience to warrant syntax. Maybe others have a different perspective.

---

<div class="post-metadata">

**Author:** ![aleen42](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aleen42/32/461_2.png) [@aleen42](https://es.discourse.group/u/aleen42)\
**Post date:** [August 25, 2021, 1:45am UTC](https://es.discourse.group/t/is-there-any-better-way-to-create-a-multidimensional-array-in-javascript/944/5 "2021-08-25T01:45:56Z")

</div>

In my opinion, it is because arrays in JavaScript are extendible, while they should be allocated a specified capacity in C/C++ first. So in many cases, we don't need to specify the capacity firstly in JavaScript, except in one case, where we need to iterate a list of natural numbers (0, 1, 2, ...) :

```javascript
const times = n => Array(n).fill('');
times(10).forEach(console.log); // => 0, 1, 2, 3, ...

```

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [August 25, 2021, 9:13am UTC](https://es.discourse.group/t/is-there-any-better-way-to-create-a-multidimensional-array-in-javascript/944/6 "2021-08-25T09:13:37Z")

</div>

I might not be up to date on the latest JS engine heuristics here but I’m sure at one point it was actually sometimes slower to pre-allocate arrays in JS because the arrays then have the wrong internal type compared to what was actually going to be stored.

So in that way better to treat them more like c++ vectors and let them naturally grow their backing storage.

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [August 25, 2021, 8:11pm UTC](https://es.discourse.group/t/is-there-any-better-way-to-create-a-multidimensional-array-in-javascript/944/7 "2021-08-25T20:11:14Z")

</div>

It's still faster in V8 at least for non-immediate arrays, but yeah, the performance gap isn't nearly large enough IMHO to merit adding multi-dimensional arrays through dedicated syntax, not when they're already relatively straightforward to code in userland.
