GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise
  • Introduction3 min read
  • Variables5 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — JavaScript Multidimensional Array - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise

JavaScript Multidimensional Array

Last Updated : 12 Jul, 2025

A multidimensional array in JavaScript is an array that contains other arrays as its elements. These are often used to represent data in a grid or matrix format.

In JavaScript, there is no direct syntax for multidimensional arrays, but you can achieve this by creating arrays within arrays.

Creating a Multidimensional Array

JavaScript doesn’t have built-in support for multidimensional arrays like some other languages. But we can still create them by making an array where each element is itself another array. This allows you to create 2D or even 3D arrays.

There are two common ways to create a multidimensional array in JavaScript:

1. Using Array Literals

The easiest and most common way to create a multidimensional array is by using square brackets ([]), also known as array literals. This method allows you to directly define arrays within arrays.

JavaScript
let matrix = [
    [101, 'Ankit', 'Noida'],
    [102, 'Ravi', 'Delhi'],
    [103, 'Sneha', 'Mumbai']
];

2. Using Array Constructor

Another way to create a multidimensional array is by using the Array() constructor.. This can be useful when you need to dynamically create arrays based on conditions.

JavaScript
let matrix = new Array(3); // Creates a new array with 3 undefined elements
for (let i = 0; i < 3; i++) {
  matrix[i] = new Array(3).fill(0); // Each sub-array has 3 elements filled with 0
}

Accessing Elements in a Multidimensional Array

To access an element in a multidimensional array, you can use the index notation array[rowIndex][columnIndex]. Remember that JavaScript arrays are zero-indexed, meaning the first element has an index of 0.

JavaScript
let mat = [
    ["a", "b", "c"],
    ["d", "e", "f"],
    ["g", "h", "i"]
];

console.log(mat[1]);
console.log(mat[2][1]);

Output
[ 'd', 'e', 'f' ]
h

Iterating Over a Multidimensional Array

To iterate over a multidimensional array, you can use nested loops. Each loop will iterate over one dimension of the array.

JavaScript
let mat = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

for (let i = 0; i < mat.length; i++) {
    for (let j = 0; j < mat[i].length; j++) {
        console.log(mat[i][j]);
    }
}

Output
1
2
3
4
5
6
7
8
9

Adding Elements to a Multidimensional Array

To add elements to a multidimensional array, you can use array methods like push(), unshift(), or directly assign values to specific indices.

  • Example: Adding a Row
JavaScript
let mat = [
    [1, 2, 3],
    [4, 5, 6]
];

// Add a new row
mat.push([7, 8, 9]);

console.log(mat);

Output
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
  • Example 2: Adding an Element at a Specific Position.
JavaScript
let mat = [
    [1, 2, 3],
    [4, 5, 6]
];

mat[1][1] = 10;

console.log(mat);

Output
[ [ 1, 2, 3 ], [ 4, 10, 6 ] ]

Removing Elements in a Multidimensional Array

To remove elements from a multidimensional array, you can use methods like pop(), shift(), or splice().

  • Example 1: Removing a Row
JavaScript
let mat = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

// Remove the last row
mat.pop();

console.log(mat);

Output
[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
  • Example 2: Removing an Element at a Specific Position
JavaScript
let mat = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

delete mat[1][1];

console.log(mat);

Output
[ [ 1, 2, 3 ], [ 4, <1 empty item>, 6 ], [ 7, 8, 9 ] ]

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.

Best Practices for Working with Multidimensional Arrays:

  • Avoid Deep Nesting: Minimize excessive nesting to keep your code maintainable. Consider using objects or maps for complex data structures.
  • Use Descriptive Variable Names: Use clear, meaningful names for arrays to make your code more understandable (e.g., gameBoard instead of arr).
  • Check Array Lengths: Always check the array's length before accessing its elements to avoid errors and undefined values.

Conclusion

JavaScript does not have built-in support for multidimensional arrays like some other programming languages, but you can simulate them using nested arrays. This approach allows you to work with grids, matrices, or even higher-dimensional data structures. With array methods like push(), pop(), shift(), and splice(), you can easily modify the elements of multidimensional arrays.

Comment

Explore

    JavaScript Basics
    • Introduction3 min read
    • Variables5 min read
    • Operators4 min read
    • Control Statements4 min read
    Array & String
    • Arrays6 min read
    • Array Methods4 min read
    • String5 min read
    • String Methods8 min read
    Function & Object
    • Functions4 min read
    • Function Expression3 min read
    • Function Overloading4 min read
    • Objects4 min read
    • Constructors4 min read
    OOP
    • OOP5 min read
    • Classes & Objects4 min read
    • Access Modifiers5 min read
    • Constructor4 min read
    Asynchronous JavaScript
    • Asynchronous2 min read
    • Callbacks4 min read
    • Promises4 min read
    • Event Loop3 min read
    • Async Await Fuction3 min read
    Exception Handling
    • Error and Exceptional Handling5 min read
    • Errors Throw & Try to Catch2 min read
    • Custom Errors2 min read
    • TypeError Invalid Array.prototype.sort argument1 min read
    DOM
    • DOM8 min read
    • DOM Elements3 min read
    • Custom Events4 min read
    • Addeventlistener5 min read
    Advanced Topics
    • Closure3 min read
    • Hoisting5 min read
    • Scope4 min read
    • Higher Order Functions7 min read
    • Debugging3 min read
    Courses
    • Placement 360 Course2 min read
    • MERN Full Stack Course2 min read
    • DSA and System Design Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.