How do you get a function to create new arrays or objects

    This function takes a number, n, and returns an n*n nested array populated with the value null.
    E.g. if n is 3, we want:
      [null, null, null],
      [null, null, null],
      [null, null, null]

How do I make a function thats create a new array and populates it?

edit: just got told to go look at Array.fill so Might be a redundant thread now lol

You can create an empty array of length n with new Array(n) and then fill it with nulls with something like myArray.fill(null)

To create an array and initialize it :

let arr = [];

use push or shift to add to the array (push adds to the back , and shift adds to the front )
nested for loops can be used to create nested arrays…

This will work:

function lineArr(n) {
  return new Array(n).fill(null);

function squareArr(n) {
  return lineArr(n).map(() => lineArr(n));

You might be tempted to do something like the following:

function squareArr(n) {
  return lineArr(n).fill(lineArr(n));

However, this second approach won’t work, and will lead to weirdness like the following:

const myArr = squareArr(2);
myArr[1][1] = 1;
//myArr is now [[null, 1], [null, 1]]

This is because array.fill fills by reference, rather than value, so the inner arrays are actually multiple references to the same array. The first approach doesn’t suffer from this problem.