JavaScript Multidimensional arrays are not supported natively with a specific syntax but they can still be created by placing arrays within arrays. This means that the elements within an array consist of more arrays.

The code example below shows how a JavaScript multidimensional array can be constructed to hold questions and answers for a quiz.

Working inwards, the outer array holds all the questions and can have more than one if comma separated. The next array holds all the elements for one specific question. These become their own comma-separated arrays. This includes the question array, an array with all the answer choices, a correct answer array and finally an array to state how many answers were considered correct. Remember arrays are zero-based.

JavaScript Multidimensional Array Example

const questions = 

    	[ [ "What does alt text stand for?" ],    //Enter Your Question 
    	"Alter ego text",    //Multiple Choice Answer (A)
    	"Alternate text",   //Multiple Choice Answer (B)
    	"Alternator text",    //Multiple Choice Answer (C)
    	"Alternating text"    //Multiple Choice Answer (D)
    	["B"],               //Correct Answers
    	[1] ],               //How Many Correct Answers for This Question

As an example, to access the correct answer manually and print it to the console, all that would need to write is:


"Alternate text",   //Multiple Choice Answer (B)

This method of holding data can be coded up to create a very advanced quiz application. The purpose of this post is just to show you what a JavaScript multidimensional array is and how it is constructed.


JavaScript Multidimensional arrays can be very useful, especially in a scenario like a quiz as so much information can be very neatly held together whilst producing short, clean and concise code that is easy to access.