#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

Multi-dimensional arrays

A multi-dimensional array is an array whose items are themselves arrays. JavaScript has no special grid type, so a two-dimensional table is simply an array of rows, where each row is an array of cells. You use them for game boards, seating charts, spreadsheet-like data, matrices and any data that naturally has rows and columns.

Building and reading a grid

You access a cell with two sets of brackets: the first picks the row, the second picks the column. Both start at zero.

const seats = [
  ['A1', 'A2', 'A3'],
  ['B1', 'B2', 'B3'],
  ['C1', 'C2', 'C3'],
];

seats[0][2];          // "A3"  (row 0, column 2)
seats[2][0];          // "C1"
seats.length;         // 3 rows
seats[0].length;      // 3 columns in the first row

seats[1][1] = 'X';    // book seat B2

for (let r = 0; r < seats.length; r++) {
  for (let c = 0; c < seats[r].length; c++) {
    console.log(`Row ${r}, col ${c}: ${seats[r][c]}`);
  }
}

Reading seats[1][1] is really two steps: seats[1] returns the second row, and [1] picks the second item of that row.

Looping with array methods

Nested for loops are clear when you need indexes. Array methods are often shorter when you transform data.

Take a marks grid where each row is one student's scores. marks.map((row) => row.reduce((sum, m) => sum + m, 0)) gives one total per student. To view the data by subject instead, transpose it with marks[0].map((_, col) => marks.map((row) => row[col])), which turns each column into a row. And marks.flat() joins all rows into a single list, which is handy for finding the overall highest score with Math.max(...marks.flat()).

For arrays nested more deeply, flat(depth) takes a number, and flat(Infinity) flattens every level.

Creating an empty grid the right way

This is where most bugs come from. It is tempting to write new Array(3).fill([]) or Array(3).fill(Array(3).fill(0)). Both create one inner array and put the same array into every row. Changing grid[0][0] then changes the first cell of every row, because they are all one array.

// Wrong: every row is the same array
const bad = Array(3).fill(Array(3).fill(0));
bad[0][0] = 1;
console.log(bad[1][0]); // 1, surprise

// Right: build a new row for each position
const good = Array.from({ length: 3 }, () => Array(3).fill(0));
good[0][0] = 1;
console.log(good[1][0]); // 0

Array.from with a function calls the function once per row, so each row is a separate array.

Other things to watch

Rows do not have to be the same length; JavaScript allows "jagged" arrays. That is fine for data like a list of students with different numbers of scores, but loops must use row.length for each row rather than assuming a fixed width.

Reading past the edge does not throw at the first level. seats[5] is undefined, but seats[5][0] throws "Cannot read properties of undefined". Check bounds, or use optional chaining: seats[5]?.[0].

Copying is also shallow by default. [...seats] copies the outer array but the rows are still shared. Use seats.map((row) => [...row]) or structuredClone(seats) for an independent copy.

Where they show up

  • Board games such as tic-tac-toe or chess.
  • Tables of results before rendering them as HTML.
  • Image pixels or heat maps.
  • Matrix maths and simple grid-based pathfinding.

Searching a grid is another place where people trip. indexOf on the outer array compares whole rows, so it cannot find a single value. Use findIndex on each row, or loop with two indexes and stop when you find the value.

Takeaway

A grid is an array of arrays read with grid[row][col]. Build empty grids with Array.from so rows are independent, loop each row by its own length, and deep-copy before changing data you want to keep.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course