const isVisited = new Array(rowLen).fill(new Array(colLen).fill(false)); // way to shallow copy two dimensional array const newIsVisited = []; isVisited.forEach((i) => newIsVisited.push(i.slice()));