How to create empty 2d array in javascript?

You can create a 6 x 6 empty array like this:

var myGrid = [...Array(6)].map(e => Array(6));
  • Array(6) generates an array with length = 6 and full of undefined values.
  • We map that array to another array full of undefined values.
  • In the end, we get a 6×6 grid full of undefined positions.

If you need to initialize the grid with a default value:

var value="foo"; // by default
var myGrid = [...Array(6)].map(e => Array(6).fill(value));

Now you have a 6 x 6 grid full of 'foo'.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)