JavaScript Multi-dimensional Arrays

JavaScript Tutorial

JavaScript Multi-Dimensional Arrays 

JavaScript supports multi-dimensional arrays, which are simply arrays inside another array.
They’re commonly used for tables, matrices, grids, and structured data.

 What is a Multi-Dimensional Array?

A 2D array looks like rows and columns (like Excel):

 Here:

  • matrix[0][1,2,3]

  • matrix[1][2]6

Accessing Elements

 Syntax:

array[row][column]

 Modify Values

Output:

[
[1, 2, 3],
[4, 50, 6],
[7, 8, 9]
]

 Loop Through a 2D Array

Using for loop

Using forEach

 Example: Student Marks Table


 

 3D Array Example (Advanced)


 

 Used in:

  • Game boards

  • Image pixels

  • Scientific calculations

Common Use Cases

  •  Tables & grids
  •  Matrix operations
  •  Game layouts
  •  Seating arrangements
  •  Timetables

Common Beginner Mistakes

  •  Forgetting second index: arr[1] instead of arr[1][0]
  •  Assuming all rows have same length
  •  Deep loops without checking length

Quick Summary

  •  Multi-dimensional arrays = arrays inside arrays
  •  Access using array[row][column]
  •  Loops are key to handling them
  •  Very useful for real-world data structures

You may also like...