var numeric = [
    ['input1','input2'],
    ['input3','input4']
];
numeric[0][0] == 'input1';
numeric[0][1] == 'input2';
numeric[1][0] == 'input3';
numeric[1][1] == 'input4';

var obj = {
    'row1' : {
        'key1' : 'input1',
        'key2' : 'input2'
    },
    'row2' : {
        'key3' : 'input3',
        'key4' : 'input4'
    }
};
obj.row1.key1 == 'input1';
obj.row1.key2 == 'input2';
obj.row2.key1 == 'input3';
obj.row2.key2 == 'input4';

var mixed = {
    'row1' : ['input1', 'inpu2'],
    'row2' : ['input3', 'input4']
};
mixed.row1[0] == 'input1';
mixed.row1[1] == 'input2';
mixed.row2[0] == 'input3';
mixed.row2[1] == 'input4';

http://jsfiddle.net/z4Un3/

And if you're wanting to store DOM elements:

var inputs = [
    [
        document.createElement('input'),
        document.createElement('input')
    ],
    [
        document.createElement('input'),
        document.createElement('input')
    ]
];
inputs[0][0].id = 'input1';
inputs[0][1].id = 'input2';
inputs[1][0].id = 'input3';
inputs[1][1].id = 'input4';

Not real sure how useful the above is until you attach the elements. The below may be more what you're looking for:

<input text="text" id="input5"/>
<input text="text" id="input6"/>
<input text="text" id="input7"/>
<input text="text" id="input8"/>    
var els = [
    [
        document.getElementById('input5'),
        document.getElementById('input6')
    ],
    [
        document.getElementById('input7'),
        document.getElementById('input8')
    ]
];    
els[0][0].id = 'input5';
els[0][1].id = 'input6';
els[1][0].id = 'input7';
els[1][1].id = 'input8';

http://jsfiddle.net/z4Un3/3/

Or, maybe this:

<input text="text" value="4" id="input5"/>
<input text="text" value="4" id="input6"/>
<br/>
<input text="text" value="2" id="input7"/>
<input text="text" value="4" id="input8"/>

var els = [
    [
        document.getElementById('input5'),
        document.getElementById('input6')
    ],
    [
        document.getElementById('input7'),
        document.getElementById('input8')
    ]
];

var result = [];

for (var i = 0; i < els.length; i++) {
    result[result.length] = els[0][i].value - els[1][i].value;
}

Which gives:

[2, 0]

In the console. If you want to output that to text, you can result.join(' ');, which would give you 2 0.

http://jsfiddle.net/z4Un3/6/

EDIT

And a working demonstration:

<input text="text" value="4" id="input5"/>
<input text="text" value="4" id="input6"/>
<br/>
<input text="text" value="2" id="input7"/>
<input text="text" value="4" id="input8"/>
<br/>
<input type="button" value="Add" onclick="add()"/>

// This would just go in a script block in the head
function add() {
    var els = [
        [
            document.getElementById('input5'),
            document.getElementById('input6')
        ],
        [
            document.getElementById('input7'),
            document.getElementById('input8')
        ]
    ];

    var result = [];

    for (var i = 0; i < els.length; i++) {
        result[result.length] = parseInt(els[0][i].value) - parseInt(els[1][i].value);
    }

    alert(result.join(' '));
}

http://jsfiddle.net/z4Un3/8/

Answer from Jared Farrish on Stack Overflow
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript array methods › javascript multidimensional array
JavaScript Multidimensional Array
November 8, 2024 - To declare an empty multidimensional array, you use the same syntax as declaring one-dimensional array: let activities = [];Code language: JavaScript (javascript) The following example defines a two-dimensional array named activities:
Top answer
1 of 15
337
var numeric = [
    ['input1','input2'],
    ['input3','input4']
];
numeric[0][0] == 'input1';
numeric[0][1] == 'input2';
numeric[1][0] == 'input3';
numeric[1][1] == 'input4';

var obj = {
    'row1' : {
        'key1' : 'input1',
        'key2' : 'input2'
    },
    'row2' : {
        'key3' : 'input3',
        'key4' : 'input4'
    }
};
obj.row1.key1 == 'input1';
obj.row1.key2 == 'input2';
obj.row2.key1 == 'input3';
obj.row2.key2 == 'input4';

var mixed = {
    'row1' : ['input1', 'inpu2'],
    'row2' : ['input3', 'input4']
};
mixed.row1[0] == 'input1';
mixed.row1[1] == 'input2';
mixed.row2[0] == 'input3';
mixed.row2[1] == 'input4';

http://jsfiddle.net/z4Un3/

And if you're wanting to store DOM elements:

var inputs = [
    [
        document.createElement('input'),
        document.createElement('input')
    ],
    [
        document.createElement('input'),
        document.createElement('input')
    ]
];
inputs[0][0].id = 'input1';
inputs[0][1].id = 'input2';
inputs[1][0].id = 'input3';
inputs[1][1].id = 'input4';

Not real sure how useful the above is until you attach the elements. The below may be more what you're looking for:

<input text="text" id="input5"/>
<input text="text" id="input6"/>
<input text="text" id="input7"/>
<input text="text" id="input8"/>    
var els = [
    [
        document.getElementById('input5'),
        document.getElementById('input6')
    ],
    [
        document.getElementById('input7'),
        document.getElementById('input8')
    ]
];    
els[0][0].id = 'input5';
els[0][1].id = 'input6';
els[1][0].id = 'input7';
els[1][1].id = 'input8';

http://jsfiddle.net/z4Un3/3/

Or, maybe this:

<input text="text" value="4" id="input5"/>
<input text="text" value="4" id="input6"/>
<br/>
<input text="text" value="2" id="input7"/>
<input text="text" value="4" id="input8"/>

var els = [
    [
        document.getElementById('input5'),
        document.getElementById('input6')
    ],
    [
        document.getElementById('input7'),
        document.getElementById('input8')
    ]
];

var result = [];

for (var i = 0; i < els.length; i++) {
    result[result.length] = els[0][i].value - els[1][i].value;
}

Which gives:

[2, 0]

In the console. If you want to output that to text, you can result.join(' ');, which would give you 2 0.

http://jsfiddle.net/z4Un3/6/

EDIT

And a working demonstration:

<input text="text" value="4" id="input5"/>
<input text="text" value="4" id="input6"/>
<br/>
<input text="text" value="2" id="input7"/>
<input text="text" value="4" id="input8"/>
<br/>
<input type="button" value="Add" onclick="add()"/>

// This would just go in a script block in the head
function add() {
    var els = [
        [
            document.getElementById('input5'),
            document.getElementById('input6')
        ],
        [
            document.getElementById('input7'),
            document.getElementById('input8')
        ]
    ];

    var result = [];

    for (var i = 0; i < els.length; i++) {
        result[result.length] = parseInt(els[0][i].value) - parseInt(els[1][i].value);
    }

    alert(result.join(' '));
}

http://jsfiddle.net/z4Un3/8/

2 of 15
31

Quote taken from Data Structures and Algorithms with JavaScript

The Good Parts (O’Reilly, p. 64). Crockford extends the JavaScript array object with a function that sets the number of rows and columns and sets each value to a value passed to the function. Here is his definition:

Array.matrix = function(numrows, numcols, initial) {
    var arr = [];
    for (var i = 0; i < numrows; ++i) {
        var columns = [];
        for (var j = 0; j < numcols; ++j) {
            columns[j] = initial;
        }
        arr[i] = columns;
    }
    return arr;
}

Here is some code to test the definition:

var nums = Array.matrix(5,5,0);
print(nums[1][1]); // displays 0
var names = Array.matrix(3,3,"");
names[1][2] = "Joe";
print(names[1][2]); // display "Joe"

We can also create a two-dimensional array and initialize it to a set of values in one line:

var grades = [[89, 77, 78],[76, 82, 81],[91, 94, 89]];
print(grades[2][2]); // displays 89
People also ask

How do you declare a multidimensional array in JavaScript?
You declare a multidimensional array in JavaScript as an array of arrays. For example, a 2D array can be declared like this: `let multiArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];`.
🌐
vaia.com
vaia.com › javascript multidimensional arrays
Javascript Multidimensional Arrays: Examples | Vaia
How do you initialize a multidimensional array with values in JavaScript?
You can initialize a multidimensional array with values in JavaScript by using nested arrays, for example: `let multiArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];`. Each inner array represents a row in the multidimensional array.
🌐
vaia.com
vaia.com › javascript multidimensional arrays
Javascript Multidimensional Arrays: Examples | Vaia
How do you iterate through a multidimensional array in JavaScript?
You can iterate through a multidimensional array in JavaScript using nested `for` loops. Each `for` loop targets a specific dimension of the array. For example, the outer loop iterates through the primary array, and the inner loop iterates through each sub-array. This allows you to access individual elements.
🌐
vaia.com
vaia.com › javascript multidimensional arrays
Javascript Multidimensional Arrays: Examples | Vaia
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-multidimensional-array
JavaScript Multidimensional Array - GeeksforGeeks
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.
Published: July 12, 2025
🌐
Programiz
programiz.com › javascript › multidimensional-array
JavaScript Multidimensional Array
In JavaScript, you can use nested loops to go through a multidimensional array: one loop for the outer array and another loop inside it for the inner arrays. For example,
🌐
Alma Better
almabetter.com › bytes › tutorials › javascript › multidimensional-array-in-javascript
Multidimensional Array in JavaScript
April 25, 2024 - A. Basic Syntax of Multidimensional ... syntax: ... In this example, we have created a two-dimensional array, where each element of the outer array is an inner array containing two elements....
🌐
Code Highlights
code-hl.com › home › javascript › tutorials
Ultimate Guide to Multidimensional Array in JavaScript | Code Highlights
February 8, 2024 - Creating a 2D array from scratch in JavaScript is straightforward. You start by declaring an empty array and then pushing sub-arrays into it: ... This loop creates a 3x3 grid with values that are the product of their row and column indices. Now that you have a solid grasp of multidimensional arrays in JavaScript, why not expand your knowledge further?
🌐
Vaia
vaia.com › javascript multidimensional arrays
Javascript Multidimensional Arrays: Examples | Vaia
You access elements in a JavaScript multidimensional array by using multiple indices corresponding to each dimension. For example, for a 2D array, use `array[row][column]` to access a specific element, where `row` and `column` are the respective indices.
Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › javascript-2d-arrays
JavaScript 2D Array – Two Dimensional Arrays in JS
November 7, 2024 - [ a1, a2, a3, ..., an, b1, b2, b3, ..., bn, c1, c2, c3, ..., cn, . . . z1, z2, z3, ..., zn ] In JavaScript, there is no direct syntax for creating 2D arrays as with other commonly used programming languages like C, C++, and Java.
🌐
WsCube Tech
wscubetech.com › resources › javascript › multidimensional-array
Multidimensional Array in JavaScript: With Examples
November 21, 2025 - Learn multidimensional arrays in JavaScript with simple explanations and examples. Understand how to create, access, modify, and more. Read now.
🌐
CodeSignal
codesignal.com › learn › courses › multidimensional-arrays-and-their-traversal-in-javascript › lessons › multidimensional-arrays-and-their-traversal-in-javascript
Multidimensional Arrays and Their Traversal in JavaScript
All indices in JavaScript arrays are 0-based. In a 1-dimensional array, the [n] notation is used to access the (n+1)th element. For example, in the array ['a', 'b', 'c'], to access the element 'b', you would use array[1] since indices are zero-based. For multidimensional arrays, each element ...
🌐
DEV Community
dev.to › who_tf_cares › how-to-work-with-multidimensional-arrays-in-javascript-9jl
How to Work with Multidimensional Arrays in JavaScript - DEV Community
January 14, 2025 - Here are some of the best methods ... nested arrays. ... Access elements in nested arrays using multiple indices. Example: array[i][j][k] ......
🌐
JavaScript in Plain English
javascript.plainenglish.io › chapter-58-mastering-multidimensional-arrays-in-javascript-a-deep-dive-e21f1281448d
Chapter 58:Mastering Multidimensional Arrays in JavaScript: A Deep Dive | by Aryan kumar | JavaScript in Plain English
October 18, 2024 - JavaScript arrays are incredibly powerful, and they become even more versatile when you start using multidimensional arrays. These are arrays of arrays, allowing you to represent complex data structures like matrices, grids, or tables. ... How to declare and initialize multidimensional arrays. How to access and modify elements. Common mistakes (❌) and best practices (✅) to avoid them. Real-world examples of working with multidimensional arrays, with full code snippets and explanations.
🌐
TutorialsPoint
tutorialspoint.com › Multi-Dimensional-Array-in-Javascript
Multi-Dimensional Array in Javascript
June 15, 2020 - You can iterate these arrays using multiple for loops. For example, let temps = [ [35, 28, 29, 31], [33, 24, 25, 29] ]; for (let i = 0; i < 2; i++) { console.log("Row #" + i) for (let j = 0; j < 4; j++) { console.log(i, j, temps[i][j]) } } ... Multidimensional arrays can have more than 2 dimensions ...
🌐
Virtualsecrets
virtualsecrets.com › misc_imgs › javascript › jsMultiDimensionalArrays.html
Javascript Multidimensional Arrays - VirtualSecrets
var theValue = changedValue; preDefinedArray[0].value1 = theValue; alert(preDefinedArray[0].value1); To see this process in action, enter a new value in the box below and click on the button: Dynamically Defined Multidimensional Array This type of multidimensional array is one that has not been defined. In this example, we need define the multidimensional array with values:
🌐
Medium
medium.com › @rabailzaheer › mastering-nested-and-multidimensional-arrays-in-javascript-with-examples-best-practices-d27fa48d219f
Mastering Nested and Multidimensional Arrays in JavaScript (With Examples & Best Practices)
February 26, 2025 - Learn how to work with nested and multidimensional arrays in JavaScript! This guide covers creation, manipulation, iteration techniques, and best practices
🌐
Dustin John Pfister
dustinpfister.github.io › 2020 › 03 › 31 › js-array-multidimensional
Multidimensional arrays in javaScript | Dustin John Pfister at github pages
November 21, 2021 - The most common way of making a multidimensional array in javaScript might be to just have arrays of arrays, or in other words just nesting arrays as elements for arrays. That is having a single array, and then have each element in that array be an array. That alone would be a 2d array of arrays at which point making each element in each nested array an array would add yet another dimension, and so on. So then in this section I will be going over just some simple examples ...
🌐
W3Schools
w3schools.com › java › java_arrays_multi.asp
Java Multi-Dimensional Arrays
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... A multidimensional array is an array that contains other arrays.
🌐
Dummies
dummies.com › article › technology › programming-web-design › javascript › understanding-multidimensional-arrays-for-coding-with-javascript-142556
Understanding Multidimensional Arrays for Coding with JavaScript | dummies
Not only can you store arrays inside of arrays with JavaScript, you can even put arrays inside of arrays inside of arrays. This can go on and on. An array that contains an array is called a multidimensional array. To write a multidimensional array, you simply add more sets of square brackets to a variable name. For example:
Author: