# Issues of filling multidimensional arrays in JavaScript

Let's consider the following code:

`let arr = []; for (let i = 0; i < 3; i++) { arr[i] = []; // pay attention to this line for (let j = 0; j < 3; j++) { arr[i].push(j + 1); } } console.log(arr);`

An important place in this code is the creation of an empty subarray. We can't omit this line, because then in the inner loop, an attempt to push data to `arr[i]` will throw an error.

The author of the following code wanted to make a two-dimensional array:

`let arr = []; for (let i = 0; i < 3; i++) { for (let j = 1; j <= 5; j++) { arr[i].push(j); } } console.log(arr);`

The written code, however, does not do what it was intended. Find and correct the author's mistake.

The author of the following code wanted to make a two-dimensional array:

`let arr = []; for (let i = 0; i < 3; i++) { arr[i] = ''; for (let j = 1; j <= 5; j++) { arr[i].push(j); } } console.log(arr);`

The written code, however, does not do what it was intended. Find and correct the author's mistake.

The author of the following code wanted to make a two-dimensional array:

`let arr = []; for (let i = 0; i < 3; i++) { arr[i]; for (let j = 1; j <= 5; j++) { arr[i].push(j); } } console.log(arr);`

The written code, however, does not do what it was intended. Find and correct the author's mistake.

The author of the following code wanted to make a two-dimensional array:

`let arr = []; for (let i = 0; i < 3; i++) { arr[j] = []; for (let j = 1; j <= 5; j++) { arr[i].push(j); } } console.log(arr);`

The written code, however, does not do what it was intended. Find and correct the author's mistake.

The author of the following code wanted to make a two-dimensional array:

`let arr = []; for (let i = 0; i < 3; i++) { arr = []; for (let j = 1; j <= 5; j++) { arr[i].push(j); } } console.log(arr);`

The written code, however, does not do what it was intended. Find and correct the author's mistake.

The author of the following code wanted to make a two-dimensional array:

`let arr = []; for (let i = 0; i < 3; i++) { arr[i] = []; for (let j = 1; j <= 5; j++) { arr.push(j); } } console.log(arr);`

The written code, however, does not do what it was intended. Find and correct the author's mistake.

← →
↑
en ru