Course contentsShow
← JavaScript
Lesson 809 of 2,72319. ArraysFree lesson

Multi-Dimensional Arrays

Create nested arrays by placing arrays inside arrays and access them with multiple brackets.

Multi-Dimensional Arrays

You'll learn how to create arrays inside arrays and access their nested elements using multiple bracket notations.

What Are Multi-Dimensional Arrays?

A multi-dimensional array is simply an array that contains other arrays as its elements. Think of it like a spreadsheet: a single array is one row of data, but if you put multiple rows together in another array, you get a grid. In JavaScript, we create these by nesting array literals inside other array literals.

Creating Nested Arrays

const grid = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
];

Here, grid is an array containing three elements, and each element is itself an array. This creates a 3×3 structure—like a tic-tac-toe board.

Accessing Nested Elements

To access elements in a multi-dimensional array, use multiple sets of brackets:

const grid = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
];

const firstRow = grid[0];      // [1, 2, 3]
const middleValue = grid[1][1]; // 5
const lastValue = grid[2][2];   // 9

The first bracket selects which inner array you want (grid[1] gets [4, 5, 6]), and the second bracket selects an element from that inner array (grid[1][1] gets 5).

Setting Nested Values

You can also modify nested elements the same way:

grid[0][0] = 99;  // Changes 1 to 99
grid[2][1] = 0;   // Changes 8 to 0

Key Takeaway: Multi-dimensional arrays are arrays containing arrays as elements, and you access nested values by chaining bracket notation—array[outerIndex][innerIndex]—where each bracket "digs deeper" into the structure.