JavaScript Multi-dimensional Arrays

JavaScript Multi-dimensional Arrays

A multi-dimensional array is an array that contains other arrays as its elements. They are often used to represent matrices, tables, or grids.


 Creating a 2D Array


 

Output:

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

 Accessing Elements

Use two indices: array[row][column]


 Modifying Elements


 Iterating Multi-dimensional Arrays

Using nested for loops

Output:

1 2 3 4 50 6 7 8 9

Using forEach()


 Example: 3D Array (Array of Arrays of Arrays)


 


 Practical Uses

  • Game boards (e.g., Tic-Tac-Toe, Chess)

  • Matrices in math and science

  • Tables and grids in web apps


Summary Table

Feature Example
2D Array let arr = [[1,2],[3,4]]
Access arr[0][1]
Modify arr[1][0] = 10
Nested loops for (i,j) or forEach
3D Arrays let cube = [[[1]]]

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *