JavaScript Multi Dimensional Array

You are Here:

JavaScript Multi Dimensional Array

JavaScript does not support multi-dimensional arrays as like C, C++, etc.

To have multi-dimensional array in JavaScript, you have to use arrays within an array.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var myCar = [["Jaguar", "XJ", "White"],["Maserati", "Ghibli", "red"]]; document.write("Car - " +myCar[0][0]); document.write("<br>Model - " +myCar[0][1]); document.write("<br>Color - " +myCar[0][2]); </script> </body> </html>

Declaring Multi Dimensional Array

In JavaScript, you have to declare the exact structure of the multi-dimensional array before initializing the value to it.

Note: This example will throw an error because we have initialized the third row without declaring it.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p>Check your console.</p> <script> //Only two rows var myCar = [[],[]]; // first row myCar[0][0] = "Jaguar"; myCar[0][1] = "White"; // Second row myCar[1][0] = "BMW"; myCar[1][1] = "red"; // Third row (Error Occurs) myCar[2][0] = "Benz"; myCar[2][1] = "Brown"; document.write("Car1 - " +myCar[0][0]); document.write("<br>Car2 - " +myCar[1][0]); document.write("<br>Car3 - " +myCar[2][0]); </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author