SHARE
Facebook X Pinterest WhatsApp

The JavaScript Diaries: Part 12

Written By
thumbnail
Lee Underwood
Lee Underwood
Apr 17, 2007

This week we look at what happens with multidimensional and associative arrays. As you look at these you will start to understand where you can use JavaScript when building your Web sites.

Multidimensional Arrays

This type of an array is similar to parallel arrays. In a multidimensional array, instead of creating two or more arrays in tandem as we did with the parallel array, we create an array with several levels or “dimensions.” Remember our example of a spreadsheet with rows and columns? This time, however, we have a couple more columns.

Multidimensional array compared to a spreadsheet column

Multidimensional arrays can be created in different ways. Let’s look at one of these method. First, we create the main array, which is similar to what we did with previous arrays.

var emailList = new Array();

Next, we create arrays for elements of the main array:

emailList[0] = new Array("President", "Paul Smith", "psmith@domain.com");
emailList[1] = new Array("Vice President", "Laura Stevens", "lstevens@domain.com");
emailList[2] = new Array("General Manager", "Mary Larsen", "mlarsen@domain.com");
emailList[3] = new Array("Sales Manager", "Bob Lark", "blark@domain.com");

In this script we created “sub arrays” or arrays from another level or “dimension.” We used the name of the main array and gave it an index number (e.g., emailList[0]). Then we created a new instance of an array and gave it a value with three elements.

In order to access a single element, we need to use a double reference. For example, to get the e-mail address for the Vice President in our example above, access the third element “[2]” of the second element “[1]” of the array named emailList.

It would be written like this:

var vpEmail = emailList[1][2]
alert("The address is: "+ vpEmail)
  1. We declared a variable, named it emailList, and initialized it with a value of a new instance of an array.
  2. Next, we created an array for each of the elements within the original array. Each of the new arrays contained three elements.
  3. Then we declared a variable named vpEmail and initialized it with the value of the third element (lstevens@domain.com) of the second element “[1]” of the array named emailList.

You could also retrieve the information using something like:

var title = emailList[1][0]
var email = emailList[1][2]
alert("The e-mail address for the " + title +" is: " + email)

Recommended for you...

The Revolutionary ES6 Rest and Spread Operators
Rob Gravelle
Aug 23, 2022
Ahead of Time (AOT) Compilation in Angular
Tariq Siddiqui
Aug 16, 2022
Converting a JavaScript Object to a String
Rob Gravelle
Aug 14, 2022
Understanding Primitive Type Coercion in JavaScript
Rob Gravelle
Jul 28, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.